Support » Fixing WordPress » List related posts in same category

  • I’ve been struggling over this for a week, but I’m just not good enough at coding PHP to do it myself. Can anyone help me? I’d like to produce a list of all the posts in the same category as the currently displayed post. For example, if the post X is in category #1, I’d like a list of all the posts in category #1 (including post X) listed in alphabetical order by name. Just the title with a permalink.
    The code I came up with only uses the current “post” call, which doesn’t work unless you are already listing everythingn in a category:

    foreach ($post as $posts) { start_wp(); //start of loop

  • " rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?>
    } //end of loop
  • I’ve tried writing a function which will do a new query, but I don’t really know how to do that correctly, and I don’t know how to integrate it into the loop written above:

    function post_by_cat() {
    global $tableposts, $posts, $id, $post, $wpdb;
    $current_category = $post->post_category;
    $catposts = @$wpdb->get_row("SELECT ID,post_title FROM $tableposts WHERE post_category='$current_category' ORDER BY post_date");

    Thanks to anyone who can help!!!
    I’m doing this to create a menu on a site with only a small number of posts per category, since it is more of a static site than a blog. However, it would probably be useful for bloggers to have a “related posts” plug-in that worked in a similar way. My goal, however, is to make a dynamic “menu” that will show all posts in a category, allowing me to only create one “hard-link” to the main post in each category from the main page.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Of course, the last tag is closed – but it won’t let me edit something twice in a row. Some kind of spam protection which only seems to affect editing posts, not actually posting…

    Two problems. #1 is this:
    SQL/DB Error:
    [You have an error in your SQL syntax near ‘ORDER BY post_status DESC, post_date DESC LIMIT 30’ at line 1]
    SELECT DISTINCT * FROM wp_posts WHERE (post_status = ‘publish’ || post_status = ‘sticky’) AND post_category= ORDER BY post_status DESC, post_date DESC LIMIT 30
    The number 2 problem is I’m not sure how to write the loop. I know it should look something like this, but I’m not sure how to modify it:
    foreach ($post as $posts) { start_wp(); //start of loop

  • ” rel=”bookmark” title=”Permanent Link: <?php the_title(); ?>”><?php the_title(); ?>
    } //end of loop

Damn, forgot to escape the HTML section – but I guess that doesn’t matter, it is the loop I need to work on.

Let’s take this offline until I can get it working for you (and update my Amazon wishlist! 😉 ), and then I will post the working solution here or the Wiki. I already see the problem the SQL error is hitting, and I can put together a quick sample for your on the rest.
Drop me a line at davebytes[AT], and I’ll put something together you can just grab direct off my site. I assume you want the related stuff as a sidebar list (i.e., [ul]+[li-per-post], like list_cats or something…).

I should note you can use get_archives, passing in a category, to get a link-list, but I’ve been assuming you want something a little more, say, ‘customizable’…
I’m working on it now, should have something in a bit…

I sent you an e-mail, I hope you got it.
The get_archives function might do the trick, I just need a link list of all the posts in the current category, not anything fancy. I don’t even need it limited as there will not be many posts in the category. But perhaps other people would find a customizable solution helpful.
– Luhmann

where is the get_archives function? I don’t see it in functions.php or in the category template functions document.

Works great! Thanks!

No problem.
If someone wants to add this to the Wiki, feel free.

I copied your post to this new page, linked from the Wiki Hacks page:

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘List related posts in same category’ is closed to new replies.