Display Related Posts for Custom Post Based On Category

Share Now

Custom Post : item
Custom Taxonomy: item-category

<?php                              
$tags = get_the_terms($post->ID, 'item-category');
if ($tags) {                             
$first_tag = $tags[0]->slug;
$args=array(
//'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'posts_per_page'=>3,
'caller_get_posts'=>1,
'post_type' => 'item', 
'item-category' => $first_tag,                               
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>


//RELATED POST TITLE
<h3><?php the_title(  ); ?></h3>


<?php
endwhile;
}
wp_reset_query();
}
?>

Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert