Display Related Posts Based on Taxonomy Term
Showing related posts on post page (single.php) can be a great way to allow users to find more interesting articles that you may have written. One way of doing this could be displaying other posts from the same category. Now, I already discussed on this topic which was entirely focused on the default “post” type. So, what about the Custom Post Type? You will get your answer in a bit, hanging in there.
So, today I will discuss how to extend such functionality for custom post type or even with custom taxonomy. Let’s get started and take a look at the snippet first.
<?php // get the current post terms $post_cat = wp_get_object_terms($post->ID,'taxonomy',array('fields' => 'ids')); // arguments for the query $args = array( 'post_type' => 'custom_post_type', 'post_status' => 'publish', 'posts_per_page' => 5, 'orderby' => 'rand', 'tax_query' => array(array( 'taxonomy' => 'category', 'field' => 'id', 'terms' => $post_cat)), 'post__not_in' => array ($post->ID)); // new query based on arguments $rp = new WP_Query($args); if ($rp->have_posts()) : echo '<ul>'; while ($rp->have_posts()) : $rp->the_post(); echo '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>'; endwhile; echo '</ul>'; endif; // reset the query wp_reset_postdata(); ?>
Sounds complicated? Allow me to explain.
This snippet is pretty efficient and can be used for both the default posts (on single.php) or with the Custom Post Type (singular.php). The same goes with the custom taxonomy or default categories associated with default posts. What we are doing at the very beginning of this snippet is to find the terms or categories associated with the current post that the readers are viewing. Afterwards, we are creating a query to find posts based on the post type and display them on a list.
Definitely you can play around with the certain parameters depending on your requirement but for the sake of simplicity I will keep my discussion limited. I am going to display 5 randomly picked post which you should understand by now simply looking at the snippet.
For Custom Post Type
If you want to display custom post type posts, all you have to do is to change the custom_post_type word with your registered post type. If you have custom taxonomy registered for your custom post type, change the taxonomy word with your custom taxonomy name. Just in case if you didn’t registered custom taxonomy and using the default category to organize your custom post type, change the taxonomy word with category and that should be fine.
Surely you can use this snippet with the default posts with default categories (single.php). In order to use it with the default posts and categories, simply change the taxonomy word with category. Also remember to change the custom_post_type with the post word within the arguments. Rest would fall in it’s own place and should show the related posts based on the current post’s category association.
Now you can style the lists as you wish using CSS markups. You can get creating with your style but you know how the lists are being generated.
I hope you would find this post useful and would be using it on your theme. If you have any questions on this regard, feel free to post your questions from the comment section below. Thanks.