Add Related Posts in Each Posts in WordPress – Info PHP


This article explains how to related posts to each post in depending on the tags given. Even though there are many plugins available to do that, here I will show you a demo of implementing it without a plugin. I hope you like it.

Please see this article in my blog.


Today I was writing some articles on my website and when I finished my writing, I was just thinking about showing the related posts to each post depending on the tags selected. Hereby I will show you how to implement it.

Before going through it, we can do this in two ways.

  • With a plugin
  • Without a plugin

I will always recommend you to use normal PHP, the WordPress implementation that does not require any plugin. Do you know why?

Limitations if we use plugins

  • It may change your theme style.
  • It may not support your framework version.
  • It may slow down your website.
  • If you include a plugin, you are giving credits to the plugin author.
  • Some plugins may have some internal redirects, it may cause unwanted loads.
  • Some plugins add ads without your knowledge.

These all problems will be resolved if you use normal few lines of PHP code.

If you still need to use a plugin, you can see some plugins here.

Using the code

Since we need the related posts entry to be seen in each and every post, we should make some changes in the file called single.. You can either edit this file or create a widget.

Determine the end of each post, you can inspect one of your page elements in the UI and determine the class name and search it in the single.php file. For me it is the class post-excerpt.

Now after the class ends, you need to write the following code:

  1. class=“relatedposts”>  



  4.         $orig_post = $post;    
  5.         global $post;    
  6.         $tags = wp_get_post_tags($post->ID);    
  7.         if ($tags) {    
  8.             $tag_ids = array();    
  9.             foreach($tags as $individual_tag$tag_ids[] = $individual_tag->term_id;    
  10.             $args=array(    
  11.                 ‘tag__in’ => $tag_ids,    
  12.                 ‘post__not_in’ => array($post->ID),    
  13.                 ‘posts_per_page’=>5,   
  14.                 ‘caller_get_posts’=>1    
  15.             );    
  16.             $my_query = new wp_query( $args );    
  17.             while$my_query->have_posts() ) {    
  18.                 $my_query->the_post();    
  19.         ?>  
  20.     class=“relatedthumb”>  
  21.         “external” href=”  
  22.             “>  
  23.             array(150,100)); ?>  


  • }    
  • $post = $orig_post;    
  • wp_reset_query();    
  • ?>  

    You can set your posts count in the following lines of code. 

    Our next step is to add some CSS styles to the links.


    You can either paste this CSS into a single.php file or in style.css.

    Now update your file and run your website. Click on any post and see the output.

    I have selected a post that is related to jQuery and got the related posts as follows. It works great!

    Please note that you need to add tags in each post, then only this mechanism works.

    Reference: Related


    That is all. I hope you liked this article. Please share your feedback with me.

    Article Prepared by Ollala Corp

    You might also like More from author

    Leave A Reply

    Your email address will not be published.