WordPress Related Posts in Sidebar


Several of you keep pestering me on how I do my ‘related posts’ thing in the sidebar. You’re nosy buggers, I’ll give you that. Anyway, here comes the confession part: they’re not really “related” posts at all. In fact, the only thing that they each have in common is that they’re in the same category. My related posts are the last 5 posts from each category that the post you’re viewing is in (whew, mouthful).

In sidebar.php, I check that the post is exactly that: a single post. I can then grab the category IDs, loop through them, and grab the post information as I go. Blah blah blah, the interesting (working) part is:

if (is_single()) {
	if (get_the_category()) {
		<p>These similar posts from the same categories may be of interest to you:</p>
		foreach((get_the_category()) as $category) {
			echo '<h3>Related Posts in '.$category->cat_name.'</h3>'."\r\n<ul>";
			while (have_posts()) {
				<li><a href="<?php the_permalink() ?>" title="permalink to <?php the_title() ?>"><?php the_title() ?></a></li>
			echo "</ul>\r\n";

You’re all welcome to help yourself, of course.

Posted in Interwebs

12 comments so far

  1. Josh said:
    On September 9, 2008 at 4:58 pm

    Thanks for sharing. How might the current post be excluded from the list? For example, the sidebar of this post lists this very post as a “related post”.

  2. Jem said:
    On September 9, 2008 at 5:37 pm

    ^ It doesn’t, although I really should mod’ it so that it does. Which I guess is what you’re asking… let me have a play and I’ll get back to you.

  3. Vasili said:
    On September 9, 2008 at 7:25 pm

    @Josh You could probably compare the ID of the post in the loop to that of the post on the page. If they are the same you can skip that once around in the loop with continue. :)

    I don’t know how you’ll be able to code that, but that’s probably what you’re aiming for.

  4. Jem said:
    On September 9, 2008 at 8:02 pm

    if ($post->ID … etc

    but I want to see if WP has it built in first .. something like &exclude=# (because they normally do)

  5. Vasili said:
    On September 9, 2008 at 8:31 pm

    You can change the query to, query_posts(‘cat=’.$category->cat_ID.’&limit=10&offset=1′);

    I’m pretty sure that will work. ;)

  6. Jem said:
    On September 9, 2008 at 9:03 pm

    Only if you’re on the latest post in the category. If you’re in another post in the same category and you offset by one, you’ll be missing a different post. :P

  7. Mat said:
    On September 10, 2008 at 12:08 am


    Youd expect something like:
    would work, however it doesnt.

    Perhaps you should patch it so it does then commit it back to wordpress’s trunk.
    (or I will :P)

  8. Aaron said:
    On September 10, 2008 at 1:34 am


    Thank you for this. I’ve tried plugins and other codes, but they always phail. I will be forever grateful. Or at least grateful for a few days.

  9. Jem said:
    On September 10, 2008 at 10:34 am

    Found a fix, will post later :D

  10. Jabed said:
    On September 10, 2008 at 4:30 pm

    Hi Jem, perhaps you could add these mini tutorials onto your tutorialtastic website. Speaking of that website when are you going to add some more tutorials :)

  11. Improved Related Posts in Sidebar — jemjabella.co.uk said:
    On September 10, 2008 at 7:43 pm

    […] you checked out the comments on yesterday’s post — WordPress Related Posts in Sidebar — you’d have noticed that the “related” posts code I use was flawed. […]

  12. Alessandro said:
    On September 20, 2008 at 12:28 am

    So good! It can be a widget.
    I think I will do it
    thanks Jem!