
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!
126 Responses
Wow! Great! Thank You
Great! This is the best code that I can find on the web!
Great…
Just what I was looking for my new project!
Thanks a lot!
@Tschai: Yes, this is particulary useful for theme developers!
@all: Thanks
Glad you like it!
Great idea to show related by using tag!
This is great, as I have so many plugins that it will be great to get rid of one.
Thanks
wow, nice.
I am sure it will boost my web performance
thank you
I have tried many plugin to show related post on single post, but none worked satisfactorily for me, since then i use to list related post manually. I hope this hack will work batter then those plugins.
For some reason, this works on some posts and shows related tags and then on others it does not work. For example, three posts may have identical tag, but only one or two of three will show others as related post….. weird.
Ahhh, maybe it is only matching if both posts have same tag as first tag?
It works , thanks !!
That’s great big thanks.
I’d like to try this tip but I have a question on tags used. Are they WordPress tags or meta tags (plugin SEO) required to work great ?
This replaces the post comments with the comments for the last related post. Is there a way to fix this?
@Maigret: Nothing more needed, the code should work as it
@The Molitor: Didn’t noticied that. I think it depends where you place the code. If it’s before the comment, you’ll have the problem, if it’s after (for exemple in the sidebar) you’ll not.
I’m thinking about a solution.
Thanks for this code, however when I use it (slightly modified to output an ordered list) it will only display the ‘Related’ list when there are no comments on a post.
I have it placed in the loop, between the main post content and the comments section. Any clues?
You mention that putting it in the sidebar would help, but then it would be outside the loop (at least in my theme – I assume this is the same for all themes though isn’t it?)
Thank you for posting this! helps cut down on plugin-itis
there is a problem, if i put this code into my present loop before comments, i found that, the comments will become the last ‘related posts’ ’s comments. i think that is because of ‘$my_query = new WP_Query’ affecting the present post’s query results, i dont know whether you have checked that.
Is there a way to modify the code so that it doesn’t only match the first tag of the post?
That saves me from using another stupid plugin bloating my wordpress. Great idea!
Any fix so far?
Just tried it out (on localhost) without any luck so far…
Does anyone know how I could get this to work, from out side the loop? I need to do this from a footer that would appear on every page / post. And what if you wanted to not show a post or page in the related information.
This code actually works like it is suppose to…..awesome, thanks!
same problem with mojitor and the other’s.. anyone knows what causes it to register with the related post’s comments?
Well, this issue is that the comments gets the last related post’s ID. But the should get the POST’s instead.
I tried to store the post id in other variable and then retrieve it again in the posts, but i was outta time and never finnished the tests. It might be a solution.
i adapted the code and made it work with multiple tags, not only the first one. check out the code here: http://www.3mind.at/2009/05/04/code-highlighting/
feel free to comment.
Angelica already mentioned what I wanted to say…This code doesn’t work properly if used within main loop because Comment form gets wrong post ID (the last one of related post if any). People, don’t use it, your comments will go to the wrong post!!!
Thanx for this, I’m really glad for every bit of code that makes an plugin unnecessary!!!
I tried, and it works.
Thank you.
thank you, I’ll try this for my wordpress blogs
This is what I am looking for, it is simple without plugin.
woooow, wonderfull
nice trick.
thanks
It works, easy to implement than any plugin!
Thanks
Does anyone success to fix the problem of post id’s?
Same problem as Molitor, Xtian and co
I have the problem with the comments, all the comments have changed to another post.
how can i fix that?
thanks for this great sire
I have a problem, related post is not showing in some of my blog posts
just like this
http://decentsms.com/english-funny-sms/funny-one-liners-jokes.html
you can see there is no related post showing
how can i fix this?
why no one reply:@
@XufYan: Probably because no-one have the solution for you
its a great info.
because no needed much space from my host
Thanks for the code, i will use it at my blog.
Great simple useful code.
Thank You so much, this is exactly what I was looking for!
Very useful recipe. Gonna use this for my site.
Thanks!
I’ve been thinking about implementing this on my personal blog via a plugin, but I was wondering… how much faster and how much time does it save to have the code directly implemented via this way? Is it a significant improvement?
Thanks,
Jean
Hey, guys! Don’t know if anyone still cares about this, but here’s a fix for the comment issues that appear with this code.
The wrong comments show up due to the fact that, while running a new query and a new loop, the current post’s ID gets mixed up with the related ones. So what you need to do is transfer the current ID, like so:
ID; ?>
… put the related posts code here; related posts show up, IDs change, comments get screwy …
ID = $save_my_id; ?>
… and the ID is back to the value it should be; comments should display properly after that.
Hope it’s been useful to someone! Cheers, and thanks for the initial code!
Aw, hell, the code got all mangled by wp in the comment earlier! I’ll try again:
so, to ’save’ the value of the ID:
ID; ?>
and to recall it once the related posts script is done:
ID = [$]save_my_id; ?>
If the code shows up ok, remember to remove the brackets if you want the code to do anything right.
It doesn’t work, i’ve try. the_ID() is function, not the variable.
I’ve placed this code in the sidebar. Sidebar including after main loop in single.php.
Great! But this is direct DB query, and if you know – if you got many readers, and they traverse your’s site freaquntly, your site become slow like turtle, maybe you shoud cache this information for every post, ant only when writing new one rebuild the cache by the TAG’s.
Thanks 4 the code
I found a very simple solution to the comments issue that I and other folks had with this code messing up the comments. Using wp_reset_query(); at the end. If you don’t the most recent wp_query dictates what will show in your comments area – if it falls after the related posts
- so all you have to do is drop in wp_reset_query(); after the endwhile;
You rock man. Thanks
Thanx for this. i will remove my wp plugin. I have already 24 plugins on my site.
Awesome, thanks a lot!
3 Words…
Smart – Sweet & Neat !
-JD
I want to thank so much the author for this useful code and even more Ian who fixed the problem about comments (and in my case also “share buttons”)
I asked everywhere about this and no-one was able to help me. thanks guys, now I fixed it
I added this with a small change – I did a check to see if there was an image to include and only tried displaying it if there was.
There has been an immediate nearly 50% bump in pageviews – wonderful!
1. Two things I would love to see – automagically pull the first image from my post without using a custom field.
2. Have the related posts go back in time from the date of the post being viewed…i.e. right now you get the same listing of the four most recent related posts all the time…I want more than a few related posts to get exposed to readers as they explore through my archives – not just the most recent four that have been posted.
This is terrific. I’m actually going to try and use it to list related posts on pages outside of my Wordpress environment.
Excellent trick to display the wordpress related post without using a wordpress plugin. thanks for sharing
@Ian – so all you have to do is drop in wp_reset_query(); after the endwhile;
Worked like a charm! Thanks to the author for this post and to Ian for the fix
can you tell , how to showing related posts only in one category ? great thanks
This script has a bug. It changes the “$post->ID”.
I discovered the solution: Add in last line: wp_reset_query();
This script has a bug. It changes the “$post->ID”.
I discovered the solution: Add in last line: wp_reset_query();
Great tip – how could I display a thumbnail with this as well… I have a custom field with “image_thumb” – I would like to display that with the related posts – I have it working with something like popular posts and recent posts…. any help is appreciated…
Hi I’m new here.
What should be changed to work with custom fields and not labels?
how about related post with thumbnails?
Chad Muller,
ID, ‘image_thumb’, true)) :
echo ‘Image: ‘.get_post_meta($post->ID, ‘image_thumb’, true);
endif;
?>
Oops… just use.. echo get_post_meta($post->ID, ‘image_thumb’, true);
Brynner – where do I add that – does it not have to be in an image tag to display the thumbnail?
Chad – Yes, put in tag img, in archive.php, per example.
Brynner – Not sure what you mean – I am not an expert with this stuff… I might be overlooking something… this is what I have – I have marked an area with ?????? – do I not have to display the image there after the echo grabs the data – it has to spit it out somewhere right??
ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
echo get_post_meta($post->ID, ‘image_thumb’, true);
$args=array(
‘category__in’ => $category_ids,
‘post__not_in’ => array($post->ID),
’showposts’=>5, // Number of related posts that will be shown.
‘caller_get_posts’=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo ‘Related Posts’;
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<a href="” rel=”bookmark” title=”Permanent Link to “>
<?php
}
echo '’;
}
}
?>
Brynner – my last comment with the code did not submit properly….
You can view my code here : play.projekt19.com/related_posts.rtf
Chad,
Your code corrected: paste.spaceonline.com.br/?q=49&ln=0
Brynner… Thats fantastic.. thanks so much… it make sense… I really appreciate it
Great piece of code! But one Question When I’ll add this in the loop. THen my comments dosent show?
I have use this script on my blog post, It works!!
Thanks..
Thanks for this awesome code. And thanks to Ian and Brynner Ferreira for the wp_reset_query(); solution.
I’m trying to figure our how to have the code look up multiple Tags and not just the first one. Thanks in advance
Anyone else having an issue with Pagebar? The pagebar is displayed directly underneath the related posts. How do I go about removing this?
Also, I added the wp_reset_query(); code at the end and still am only getting related posts to the first tag.
Any help would be appreciated
Trackbacks: