Displaying Related Posts Based on Categories

I spent last night playing around with WP_Query. In the past, I wrote a post explaining how to display random posts both for the Posts and for Custom Post Type. Today I will focus on displaying related posts based on current post category or categories.

Displaying related posts based on a fixed category ID or slug is not that big of a deal, however it becomes more complicated when your post on “single.php” page has multiple categories and it changes every time you see a post from a different category. That’s exactly where this snippet comes in.

No matter whatever the post your are reading or whatever the category it belongs to, this snippet will find the other posts from the same category and will exclude the post that you are currently reading.

All you have to do is to copy this snippet and place it on your theme’s single.php page. I also assigned a div class called “related” which you can style anyway you want from your theme’s style.css file. So folks, here is the code. Feel free to test or use it on your site.

global $post; 
$categories = get_the_category($post->ID); 
$category_ids = array(); 
foreach($categories as $category) { 
   $category_ids[] = $category->term_id; }
   $args = array(
      'category__in' => $category_ids,
      'post__not_in' => array($post->ID),
      'posts_per_page'=> 5,
      'orderby' => 'rand'); 
   $query = new WP_Query($args); 
      if($query->have_posts()) :
         echo '<div class="related">';
         echo '<h4>Related Posts</h4>';
         echo '<ul>'; 
      while($query->have_posts()) : $query->the_post();
         echo '<li><a href="'.get_permalink($post->ID).'">';
         echo get_the_title().'</a></li>';
         echo '</ul>';
         echo '</div>';
      else :
         echo 'No post found!';

Sorry! I couldn’t make it smaller than this.


This snippet might sound a bit overwhelming for you but allow me to explain it to you. At the very beginning of our snippet we initialized the global $post variable so that we can retrieve information about the current post that is being viewed.

I assigned $categories variable to grab the categories that the single post belongs to. I also assigned $category_ids variable to be an array because we may need to deal with multiple post categories. Through a foreach loop I retrieve every single category and their term ID because get_the_category function returns array of category objects.

Finally I queried posts (WP_Query) with custom arguments to find posts that belongs to current post categories excluding the current post. Then I simply loop through the posts and display them as a list item. Last but not least I called for wp_reset_postdata function to restore the main query of the page on single.php.

This is as simple as this gets. Let me know your thoughts on this and feel free to post any question from the comments section.

Today In History



Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.