Add Related Posts to WordPress without a Plugin

Over the last month I’ve implemented quite a bit of changes to the site, including adding a section to each post that displays links to related posts. I use WordPress for this website and I thought about looking for a plugin that would do the trick. However, I’ve been trying to cut down how many plugins I use to make it easier to upgrade and I came up with an alternative solution that works great.
I decided to add PHP code to the single.php file in the WordPress theme that I use. It goes through all of the tags that were defined for the current post and gets five posts that use the same tag.
I’ve taken the code provided on this website and tweaked it so that it gets the related posts for all of the tags and not just the first one.
<?php
$original_post = $post;
$tags = wp_get_post_tags($post->ID);
if($tags)
{
echo '<h4 class="content-title">Related Posts</h4>';
$sendTags = array();
foreach($tags as $tag)
$sendTags[] = $tag->term_id;
$args = array(
'tag__in' => $sendTags,
'post__not_in' => array($post->ID),
'showposts' => 5,
'caller_get_posts' => 1,
);
$queryDb = new WP_Query($args);
if($queryDb->have_posts())
{
echo '<ul>';
while ($queryDb->have_posts()) {
$queryDb->the_post();
?><li><a href="<?php the_permalink() ?>"
rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?></a></li><?php
}
echo '</ul>';
}
}
$post = $original_post;
wp_reset_query();
?>
Updated 2/19/2009: I discovered a bug in the original code I posted that caused a problem with the comments. Just add the first line and the last two line to the previous code and it will fix it.
Did you like this article?
Please help us spread the word about this site by tweeting, digging and sharing this article around the web (see the buttons immediately above).
15 Comments or Pings to Add Related Posts to WordPress without a Plugin
Trackbacks & Pings
Leave a Comment or a Question
Most Popular
Topics
- PHP Programmer Tips(16)
- Project Management(10)
- WordPress Tips(9)
- Sample PHP Code(9)
- Work Smarter(8)
- Programming Advice(4)

Thanks so much for this code. After trying several hours to implement similar codes from other sites, yours was the only accurate. Kudos! I posted and linked to you.
Question: How can I add a test if there is no related posts?
Thanks again!
How can we use these codes to show the posts with similar titles instead of tags? Is there any solution?
You would probably just have to loop through all posts and then do a regular expression search against the post title to do what you want.
Thanks for reply. This is probably great tip from you, but I dont understand very well PHP programming, so, I dont know how to do it. Whenever, if you write this in the future, appriciated! Thanks in advance. I follow this topic.
As i am not much good in PHP i was searching this script for my blog and i finally got it here thank you i will follow the topic thanx once again
This is really work with my themes. Thx a lot
Thanks a million for posting this! Helped me a lot!
Thanks a lot. We was looking for a better related posts snippet from a long time and found it finally!
The idea in the two latest lines was very simple, but very good
TY. I have test this code, and work perfectly on my blog. How about related posts based on category?
terima kasih banyak . . .
thanks a lot
i hope it works on the new version