Category Based Related Posts For WordPress

/ Wordpress / by Paul Robinson / 9 Comments
This post was published back on May 22, 2010 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

Depending on the way you are using WordPress this may, or may not, work well for you. It kind of relies on the fact that posts will be related because they are in the same category. If for whatever reason posts can be unrelated to each other, although they are in the same category, you’ll probably want to use a plugin like YARPP.

The Idea!

We want to show the related posts at the bottom of the post on single.php (kinda like I do), but use no plugins. To do this we are going to ask WordPress what category the current post is in, and then use it to make a custom loop. Got it? Good, let’s go.

The Code!

First we need to ask WordPress what category the current post is in.

This will give us back an array, with one entry per category, containing an object with that category’s information within it. We also need to know which post we are looking at so we can exclude it from our results.

Now let’s add that together with a call to get_posts().

There we have it. In get_posts(); we have asked for 3 posts, from the current category, and excluding the current post. $category is an array with an object inside hence the need for the [0] on the variable.

The Limitations!

Well as I said before one major limitation is that it will never be as good as a related plugin like YARPP due to the special scoring system it uses. However it is great for certain websites/blogs. You may also notice that it only gets posts from the first category attached to the post, you can do something about that should you want to.

That will create a comma separated list of the category IDs which is how get_posts() expects it. It will however only do it should it detect that the category array has more than one entry saving a bit of time & memory.

Sadly I can’t provide an example for this one. However as always if you have any questions, or suggestions please let me know using the comments.


Author’s gravatar

Hi, thanks a lot for the idea. But I’m having a little trouble with the code. I wanted to list all posts from the current post’s category and highlight the title of the current post in the list. To do that I changed your code to this:

But in the end: The links on the list that I get doesn’t change the content of the current post. The post and the URL, they both change, but the content stays the same.
Any ideas?
Thanks in advance.

Author’s gravatar author

Do you mean that the_permalink() is giving you the wrong link?

You might need to run setup_postdata($post); just after the start of the foreach. I’ve never had too on my own blog, but I know a few I’ve worked on have required it.

If that’s not what you meant get back to me & I’ll see what else I can do to help.

Author’s gravatar

No, the_permalink() is giving the right link, but the content just doesn’t change when you click on it. Anyway, instead of searching for a problem in the code, I found a different solution to my problem. Here, check it out:

Thank you, your article gave me the inspiration to do this. Take care.

Author’s gravatar author

Wow. Okay, I get what you meant now.

Nice way of working round it. 😉

Author’s gravatar

Is there a way to add the first image from each of the posts next to the title?
I am looking to make a list of each post under that particular category with its first image as a thumbnail. Could this work?

Author’s gravatar

Hi Nicole,

The easiest way to do this is by upgrading to WordPress 3.0 (if you are using a version less than 2.9 or haven’t already) and use the new custom thumbnail feature.

First you will need to activate the feature by editing the functions file. You can do this by following the instructions in our Custom Thumbnail Tutorial (at the bottom under the heading The Post Thumbnail) … This method is not retroactive and won’t make thumbnails for existing images (only ones you upload after you add the code). You’ll find a link to a plugin to auto generate the thumbnails for existing images in the tutorial.

To display the thumbnail all you need to do is use the following in the place where you want it to show (remembering to keep it in the loop)

Just remember that the bit in apostrophes needs to be the same as the name you gave your custom thumbnail function.

If you want to make it a link wrap an <a> around it like the one in the sample code at the top of this page. Finally, use CSS and extra <div> if needed to style it how you want.

Author’s gravatar

Thanks so much! Do I have to use this on every post or can I use it on one post only (i.e. list other posts in that category on one post only)?

Author’s gravatar author

Well by the nature of WP templates it will automatically display on all single posts, but if you know the ID of the post you could wrap the entire code in a conditional like this:

Where ’11’ is the ID of the post you only want the code to display on.

You can also use the post slug (that’s the one that appears in the URL separated by hyphens) or the Posts title, but if you have two posts with the same title it can cause problems so I’d use the ID.

Hope that helps you out, and thank you to LisaMarieArt for answering before. 🙂

Older Comments
Newer Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more