Introduction
This article explains how to add related posts to each post in Wordpress 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.
Background
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?
- 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.php. 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:
- <div class="relatedposts">
- <h3>Related posts</h3>
- <?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'=>5, // Number of related posts to display.
- 'caller_get_posts'=>1
- );
- $my_query = new wp_query( $args );
- while( $my_query->have_posts() ) {
- $my_query->the_post();
- ?>
- <div class="relatedthumb">
- <a rel="external" href="
- <? the_permalink()?>">
- <?php the_post_thumbnail(array(150,100)); ?>
- <br />
- <?php the_title(); ?>
- </a>
- </div>
- <? }
- }
- $post = $orig_post;
- wp_reset_query();
- ?>
- </div>
- 'posts_per_page'=>5
Our next step is to add some CSS styles to the links.
- <style>
- .relatedthumb a {
- color: #7a7a7a;
- text-decoration: none;
- }
- .relatedthumb a:hover {
- color: #01a821;
- }
- </style>
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 Posts
Conclusion
That is all. I hope you liked this article. Please share your feedback with me.

Santhakumar MunuswamyPosted Jul 17, 2015, 4:42 PM
Thanks for nice article:)
Sibeesh VenuPosted Jul 17, 2015, 11:04 AM
Rajeesh Menoth Thank you
Sibeesh VenuPosted Jul 17, 2015, 11:03 AM
Nilesh Jadav Thank you
Sibeesh VenuPosted Jul 17, 2015, 11:03 AM
Yatendra Sharma Thank You
Rajeesh MenothPosted Jul 17, 2015, 9:53 AM
Good Information Sibeesh Venu
Nilesh JadavPosted Jul 17, 2015, 6:16 AM
Nice one sir
Yatendra SharmaPosted Jul 17, 2015, 6:10 AM
gud 1
Sibeesh VenuPosted Jul 17, 2015, 5:59 AM
Gopi Chand Thank you
Gopi ChandPosted Jul 17, 2015, 5:55 AM
Nice one Sibeesh Venu