AnswerBun.com

Showing related post for custom taxonomy

WordPress Development Asked by Luca on November 28, 2020

I need to edit single.php file in order to show related post for a custom taxonomy I’ve already created (named ‘tag-italia).

Can anyone help me to achieve that?

For the "general" related post by tag I’m using this code, which shows the related post only if there’s something to show:

<div>
<?php $orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>4, // Number of related posts that will be shown.
'ignore_sticky_posts'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {

echo '<div><h3 class="titolo-related">Potrebbe interessarti:'; the_tags(' #', $sep = ' | #'); echo'</h3>'?>

<?php while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<div>
<div><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a></div>
<div>
</br><h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt() ?>
</div>
</div>

I’ve tried to customize this code without any success.

Thanks in advance!

One Answer

Try this

  $tags = wp_get_post_tags($post->ID);
  $tag_ids = [];
  foreach($tags as $individual_tag){
     $tag_ids[] = $individual_tag->term_id;
   }

   $args=array(
      'tag__in' => $tag_ids,
      'post__not_in' => array($post->ID),
      'posts_per_page'=> 4,
    );
    $my_query = new WP_Query( $args );
    
   //Check
    echo '<pre>';
    print_r($my_query);
    echo '</pre>';

Answered by Sobir on November 28, 2020

Add your own answers!

Related Questions

No more get_option(‘date_time’) in WordPress 5.5?

1  Asked on February 10, 2021 by wittich

       

How should I tackle this situation?

0  Asked on February 9, 2021 by remus

   

How to create an anonymous forum?

0  Asked on February 8, 2021 by keyrands

   

Detect if REST API is running

2  Asked on February 8, 2021 by tom-j-nowell

 

Create a bulk action to pass user profiles into a CPT

0  Asked on February 6, 2021 by zaesar

     

Can’t update WordPress with Admin Users

0  Asked on February 4, 2021 by charlene-the-web-design-queen

       

Get Child Category only

1  Asked on February 2, 2021 by joamika

   

MULTISITE: Password issue and Error Cant access site

3  Asked on January 31, 2021 by msn_administrator

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir