
Displaying related posts is a very great way to help visitors staying longer on your blog. You can use a plugin, but you also can use tags and a custom code to show related posts. Let’s see how to do!

Displaying related posts is a very great way to help visitors staying longer on your blog. You can use a plugin, but you also can use tags and a custom code to show related posts. Let’s see how to do!
This code will display related posts based on the current post tag(s). It must be pasted within the loop.
<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
}
?>
Thanks to MichaelH for this awesome piece of code!
24 Responses
Not work on WordPress 3.2.1
As of today, it’s working great with my site.
Oh, and I’m using WordPress 3.2.1.
Thanks!
Now, if only it would show a thumbnail of each post, it’d be 100%.
You can use thumbnail by including this in your loop:
“
Sorry – that code has come out completely messed up.
you can see the full text at
http://photos-for-you.com/single.php.txt
Thanks for the great piece of code! Via the WordPress forum, I found some extra code to adapt it for showing multiple tags. However, I’m running into a problem I cannot fix: I’m using posts with several tags. The effect is that some posts are turning up twice in the results. I’ve checked the loop page on WordPress, but cannot get the code right. Do you have any idea on how I can fix this? You can see the code I’m using here:
http://pastebin.com/QaUjX1u5
Why only with Tags? Why not with categories? I mean, if anyone can update the code to display related posts by category, would appreciate it a lot. Thanks for any help you can provide.
Not work on WordPress 3.2.1 – +1
It does not work in the latest version of WP
You should add wp_reset_query(); at the end to reset custom loop created and back to the main loop.
This snippet works fine on WordPress 3.2.1. Make sure you put it in the loop.
Works on version 3.3.1!!! Thanks.
Works on version 3.3.1 Thank mate
Thanks for the code MichaelH ….:)
thank you for this code
can u tell me how to make it work with category instead of tags?
Put this in. Now my website doesn’t show up and I can’t get to my wp-admin dashboard. Help.
I have been attempting to use the YARPP but continually get errors. Not sure what’s wrong… I’ve been thinking about going manually. I appreciate your post. I am still trying to figure out the CSS but this is a point in the right direction. Thanks!
thanks for this code. i want to related category post. can you give me this code??
As ArmNo said, it should have “wp_reset_query();” in the end, just like that:
}
}
wp_reset_query();
?>
Without that, the Disqus (comment system) was getting the comment from the post inside that code
It doesn’t work for word press 3.4.2
same as handelsregistera here.! it doesn’t work
but i had another simple methods.
Sir, Article is Nice… I want to know that whether it is possible to show list of posts with same tag in sidebar sorted by published date.
Awesome.
Thanks for sharing
working code for 3.4:
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$args=array(
'tag_in' => array($tags),
'post_not_in' => array($post->ID),
'showposts' => 5,
'ignore_sticky_posts' => 1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="" rel="bookmark" title="Permanent Link to ">
<?php
endwhile;
}
}
not work on ver 3.5.1
Where to paste this code. How can i use this code . I am a newbie Please illustrate with example ?
Trackbacks: