Post Pic

How to: Show related posts without a plugin

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!

145 Responses

Mar 23 2009 10:34

Wow! Great! Thank You :)

Mar 23 2009 10:59

Great! This is the best code that I can find on the web!

Mar 23 2009 10:59

Great…

Just what I was looking for my new project!

Thanks a lot!

Mar 23 2009 14:44

@Tschai: Yes, this is particulary useful for theme developers!

@all: Thanks :) Glad you like it!

Mar 23 2009 18:28

Great idea to show related by using tag!

Mar 24 2009 04:17

This is great, as I have so many plugins that it will be great to get rid of one.
Thanks

Mar 30 2009 03:08

wow, nice.

I am sure it will boost my web performance :)

thank you

Mar 31 2009 14:28

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.

Apr 01 2009 19:13

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.

Apr 01 2009 19:26

Ahhh, maybe it is only matching if both posts have same tag as first tag?

Apr 06 2009 14:03

It works , thanks !!

Apr 10 2009 12:13

That’s great big thanks.

Apr 12 2009 13:07

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 ?

Apr 12 2009 22:44

This replaces the post comments with the comments for the last related post. Is there a way to fix this?

Apr 12 2009 22:55

@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.

Apr 15 2009 16:57

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?)

Apr 16 2009 05:45

Thank you for posting this! helps cut down on plugin-itis ;)

Apr 17 2009 13:26

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.

Apr 17 2009 22:55

Is there a way to modify the code so that it doesn’t only match the first tag of the post?

Apr 18 2009 11:28

That saves me from using another stupid plugin bloating my wordpress. Great idea!

Apr 18 2009 14:24

Any fix so far?

Apr 18 2009 21:33

Just tried it out (on localhost) without any luck so far…

Apr 28 2009 17:41

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.

May 01 2009 09:17

This code actually works like it is suppose to…..awesome, thanks!

May 03 2009 15:35

same problem with mojitor and the other’s.. anyone knows what causes it to register with the related post’s comments?

May 03 2009 19:21

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.

May 04 2009 16:29

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.

May 13 2009 12:39

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!!!

May 20 2009 14:16

Thanx for this, I’m really glad for every bit of code that makes an plugin unnecessary!!!

May 30 2009 18:23

I tried, and it works.
Thank you.

Jun 04 2009 16:54

thank you, I’ll try this for my wordpress blogs

Jun 04 2009 16:59

This is what I am looking for, it is simple without plugin.

Jun 10 2009 00:48

woooow, wonderfull

Jun 10 2009 14:58

nice trick.
thanks

Jun 13 2009 17:03

It works, easy to implement than any plugin!

Thanks :)

Jun 15 2009 21:56

Does anyone success to fix the problem of post id’s?

Same problem as Molitor, Xtian and co

Jun 16 2009 19:35

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

Jun 17 2009 13:53

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?

Jun 18 2009 12:51

why no one reply:@

Jun 18 2009 12:54

@XufYan: Probably because no-one have the solution for you :(

Jun 19 2009 17:54

its a great info.
because no needed much space from my host

Jun 23 2009 05:56

Thanks for the code, i will use it at my blog.

Jun 23 2009 06:12

Great simple useful code.

Jul 21 2009 20:08

Thank You so much, this is exactly what I was looking for!

Jul 23 2009 07:09

Very useful recipe. Gonna use this for my site.
Thanks!

Jul 24 2009 09:36

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

Jul 30 2009 12:39

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!

Jul 30 2009 12:44

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. :)

Aug 01 2009 13:23

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.

Aug 05 2009 00:21

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.

Aug 16 2009 09:38

Thanks 4 the code

Aug 26 2009 19:13

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;

Sep 05 2009 06:08

You rock man. Thanks

Sep 18 2009 13:42

Thanx for this. i will remove my wp plugin. I have already 24 plugins on my site.

Sep 30 2009 16:21

Awesome, thanks a lot!

Oct 16 2009 13:31

3 Words…

Smart – Sweet & Neat !

-JD

Oct 21 2009 18:29

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 :)

Oct 22 2009 18:16

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.

Nov 29 2009 05:49

This is terrific. I’m actually going to try and use it to list related posts on pages outside of my WordPress environment.

Dec 11 2009 13:16

Excellent trick to display the wordpress related post without using a wordpress plugin. thanks for sharing

Dec 15 2009 00:22

@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

Dec 16 2009 18:07

can you tell , how to showing related posts only in one category ? great thanks

Dec 26 2009 20:00

This script has a bug. It changes the “$post->ID”.

Dec 26 2009 21:19

I discovered the solution: Add in last line: wp_reset_query();

Dec 26 2009 21:21

This script has a bug. It changes the “$post->ID”.
I discovered the solution: Add in last line: wp_reset_query();

Dec 31 2009 20:57

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…

Dec 31 2009 21:44

Hi I’m new here.
What should be changed to work with custom fields and not labels?

Jan 03 2010 13:21

how about related post with thumbnails?

Jan 03 2010 20:47

Chad Muller,

ID, ‘image_thumb’, true)) :
echo ‘Image: ‘.get_post_meta($post->ID, ‘image_thumb’, true);
endif;
?>

Jan 03 2010 21:08

Oops… just use.. echo get_post_meta($post->ID, ‘image_thumb’, true);

Jan 05 2010 03:05

Brynner – where do I add that – does it not have to be in an image tag to display the thumbnail?

Jan 05 2010 16:53

Chad – Yes, put in tag img, in archive.php, per example.

Jan 05 2010 20:05

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 '’;
}
}
?>

Jan 05 2010 20:09

Brynner – my last comment with the code did not submit properly….

You can view my code here : play.projekt19.com/related_posts.rtf

Jan 06 2010 16:03

Chad,
Your code corrected: paste.spaceonline.com.br/?q=49&ln=0

Jan 06 2010 16:11

Brynner… Thats fantastic.. thanks so much… it make sense… I really appreciate it

Jan 08 2010 20:59

Great piece of code! But one Question When I’ll add this in the loop. THen my comments dosent show?

Jan 22 2010 15:23

I have use this script on my blog post, It works!!

Thanks..

Jan 22 2010 18:26

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

Feb 02 2010 06:58

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

Feb 16 2010 16:39

Great code Michael, I have been looking for this rather then get a plugin that will just frustrate me and end up not working. Thank you.

Feb 17 2010 10:57

Just cross by and see this article,
nice simple script man, especially when you already install a bunch of plugin in your blog.
ill try this on mine …..

cheers

Feb 22 2010 04:41

@The Molitor: Might solve the problem. Last line this code.
endwhile; } wp_reset_query(); } // Reset the query obtained.
This code will remove the query.

I’m sorry in poor English.Understand?

Feb 24 2010 13:50

Thanks for the code and this is what i looking for… thanks again…

Mar 19 2010 09:32

Great….. Thanks. I love it.

Mar 27 2010 22:07

Thanks for this code, great post. Thanks a lot

Mar 30 2010 00:07

Thanks a lot, for this code.

Apr 01 2010 19:03

thankz for this trick :)

i will use for my website

Apr 04 2010 22:11

I have used this code in my blog for some time now. The problem is, that WordPress sorts all posts tags alphabetically, causing criteria for the related posts be effected by this sorting.
For example, if I write a post about SEO and I tag it “SEO” but also “Blogging”, I will get related posts that match “Blogging” tag even though SEO is more relevant…
Is there a way to prevent WP from doing this auto sorting of tags and retain the original order I added them? thanks

Apr 24 2010 14:55

Great code! But, I would like also display the excerpt for each related post (not only the title). Is it possible and how?

Many thanks,
Dem.

May 27 2010 07:42

Thanks for the tips. It will also speed up wordpress since no plugin call is needed

Jun 02 2010 14:31

Thanks this is just what I needed, I like that it degrades gracefully by not showing anything if there are no related posts with the same tags.

I also much prefer not having to use plugins where possible.

Jun 07 2010 19:29

I have the same question as @Omer Greenwald. I’ll reiterate here:

“The problem is, that WordPress sorts all posts tags alphabetically, causing criteria for the related posts be effected by this sorting.

Is there a way to prevent WP from doing this auto sorting of tags and retain the original order I added them?”

Jun 19 2010 06:37

The code still work with wordpress 3.0, nice :)

Jul 06 2010 03:42

How can I make this work as a list instead of a paragraphs? Also.. is there a way put a message like “The are no related post” so it doesn’t look empty when there is no related post.

Thanks

Jul 07 2010 00:18

I want to give you a big thanks for that post. I have been pulling my hair out all week trying to work that one out.
Your a life saver!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required