[resolved] how to exclude one category from recent posts? (11 posts)

  1. llizard
    Posted 10 years ago #

    I am wanting to exclude one category of posts from the recent posts. I was hoping that a plugin would not be necessary and have searched as best I can to find unsatisfactory results:

    I find the reply "use a custom query to filter out results. Check the codex" to be particularly irritating in this thread
    It would have been much more useful if the person had pointed out WHERE in the codex to look. As it happens, I have looked in the codex as best I can. But I really don't have that great a grasp on PHP so it is very time consuming for me to comprehend something enough to even be able to tell if it might work.

    I hoped that I could somehow use wp_list_cats('&exclude=4') in

    <?php get_recent_posts(); ?>

    something along the lines of get_recent_posts(&exclude=4); ????

    Any ideas?

  2. 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: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

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

  3. mkallen
    Posted 10 years ago #

    I use this on my home page:

    if (is_home()) {

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

  4. llizard
    Posted 10 years ago #

    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?

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

  6. llizard
    Posted 10 years ago #

    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.

  7. llizard
    Posted 10 years ago #

    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)

  8. llizard
    Posted 10 years ago #

    Excuse me for replying to myself...

    Finally!!! I applied rembem's solution outlined at http://wordpress.org/support/topic/52819

    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.

  9. "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.

  10. llizard
    Posted 10 years ago #

    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.

  11. zonetrap
    Posted 9 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic