Support » Fixing WordPress » how to exclude one category from recent posts?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    There is no get_recent_posts() function, so I assume you meant wp_get_recent_posts(). But no, it has no way to exclude by category.

    You’re on the right track with this link:

    Something like this should work. You’ll need to modify it to fit your needs (and possibly to fix bugs from my seat of the pants code)…

    $myrecentposts = $wpdb->get_results(
    "SELECT DISTINCT * FROM $wpdb->posts, $wpdb->post2cat".
    "WHERE post_date <= '$now' ".
    "AND (post_status = 'publish') ".
    "AND $wpdb->posts.ID = $wpdb->post2cat.post_id ".
    "AND $wpdb->post2cat.category_id != YOUR_CATEGORY_NUMBER_TO_EXCLUDE ".
    "GROUP BY $wpdb->posts.ID ".
    "ORDER BY post_date DESC ".
    foreach ($myrecentposts as $post) :
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>

    I use this on my home page:

    if (is_home()) {

    It excludes the 12th category from my home page, but includes it in archives.

    Thank you both for your replies.

    You’re absolutely right, Otto42, there isn’t a get_recent_posts() function. I had forgotten that I have the recent-posts plugin installed. Until a few moments ago, it was version 1.05 I just downloaded the latest version and installed that in hopes that there might be something there. Alas no. And there doesn’t seem to be anything useful (to me, anyway) in the read-me file either.

    I see that wp codex: Displaying Posts Using a Custom Select Query says I must have at least one post with Custom Fields data.[… and …]created a Page and associated a Page Template with that page.

    It just doesn’t get easier does it? :^/

    Have you placed that code on your theme index.php, mkallen?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    llizard: The codex is only referring to their example code when they say that. You don’t need a custom field to select on for you to make a custom query. You don’t need a special page. Try the code I posted above. It should work.

    Let me preface that I’m never thrilled to just apply code without really knowing what it means. But I’ve just now tried your coding, Otto42, and got a major error message.

    WordPress database error: [You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '<= '' AND (post_status = 'publish') AND wp_posts.ID = wp_post2c]
    SELECT DISTINCT * FROM wp_posts, wp_post2catWHERE post_date <= '' AND (post_status = 'publish') AND wp_posts.ID = wp_post2cat.post_id AND wp_post2cat.category_id !=4GROUP BY wp_posts.ID ORDER BY post_date DESC LIMIT HOW_MANY_POSTS_YOU_WANT

    Warning: Invalid argument supplied for foreach() in ../wp-content/themes/ejm/sidebar.php on line 37

    I have just checked and these are the versions I am running.
    PHP version: 4.3.11
    MySQL version: 4.0.26

    It looks like I’ll have to do some more staring at various pages.

    Even more than I did before, I wholeheartedly agree with Stahn, who wrote, “It would have been SO MUCH easier if get_archives had an option to exclude categories…” in the Exclude Category from Recent Posts in sidebar thread.

    I thought I’d try mkallen’s solution that is explained at

    Rats. It does not exclude the category from the “recent posts list” on the sidebar. (recent-posts plugin)

    Excuse me for replying to myself…

    Finally!!! I applied rembem’s solution outlined at

    This is the code that I have placed on my sidebar that excludes category 4 from showing up in the recent posts list:

    <li id="recent_posts"><h2><?php _e('recent posts '); ?></h2>
    <?php $temp_query = $wp_query; query_posts('&cat=-4'); //ejm exclude category 4 ?>
    <?php while (have_posts()) { the_post(); ?>
    <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to &ldquo;<?php the_title(); ?>&rdquo;"><?php the_title(); ?></a></li>
    <?php } $wp_query = $temp_query; ?>

    with the added advantage that I no longer have to use the recent-posts plugin.

    Thanks all for your patience. And many thanks to rembem for outlining the solution that worked for me too.

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    “But I’ve just now tried your coding, Otto42, and got a major error message.”

    Hah. Yes, the problem is that I forgot to add a space between wp_post2cat and the quote mark following it. Add that space and the problem goes away.

    The query_posts solution works except that if you don’t do it properly, it can screw up The Loop with some themes. If your sidebars occur after The Loop, then it’s fine.

    Thanks for the warning about the query_posts solution. Currently, my sidebar DOES occur after the Loop. If I make a change to place it before the loop, I’ll try to remember rather than panic.

    Thank you again for the help.

    I know IU can use this code below to exclude a category from my home page:

    if (is_home()) {

    But my index looks like this:
    ‘<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
    <div class=”post”>
    <?php require(‘post.php’); ?>
    <?php comments_template(); // Get wp-comments.php template ?>
    <?php endforeach; else: ?>’

    How can I add the first part of the code to my index code?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘how to exclude one category from recent posts?’ is closed to new replies.