WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Exclude category from the loop?

[Resolved] Exclude category from the loop?

  • I’d like to exclude all posts of a certain category from appearing on the front page, although I would like to be able to view them as single entries.

    However, I also need to preserve the maximum number of posts shown per “blog page,” which you can set under Options > Reading.

    The problem with the approach outlined here is that if you’re excluding posts of category 3, if your max number of posts shown per page is 10, and if 8 of the 10 most recent posts are of category 3, then you’ll end up with only 2 posts on your first page, instead of 10.

    What can I do to get around this?

    Thanks for your help.

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

    @otto42

    WordPress.org Tech Dude

    Check out this plugin: http://ryowebsite.com/?p=46

    It lets you exclude categories from the front page, archives, rss, etc.. It also satisfies your requirement of not limiting the number of posts when it excludes them.

    Thanks Otto42. That plugin did some stuff I didn’t want, but it helped by pointing me in the right direction.

    Actually, I discovered that there’s a pretty simple plugin fix to do what I want, as long as you’re using MySQL 4.1 or newer (older versions don’t work because they don’t support subqueries):

    function modify_where_clause($where) {
    global $wp_query, $wpdb;
    if ($wp_query->is_home && !strstr($where,'category_id'))
    $where .= " AND ID NOT IN (SELECT post_id FROM $wpdb->post2cat WHERE category_id=2)";
    return $where;
    }
    add_filter('posts_where','modify_where_clause');

    This removes all posts of category id 2 from the loop and doesn’t interfere with specific requests you might have for category 2 (in my case, a sidebar blog of only category 2 entries).

    can’t you use query_posts to handle this as well?

    How?

    http://codex.wordpress.org/Template_Tags/query_posts

    I don’t know if that info is outdated but it has worked for me.

    Okay, I see what you’re saying. The problem is that something like query_posts("cat=-3"); excludes a post of category 3 only if that post is under just category 3. If you’ve assigned it other categories, then it’s not excluded.

    @filosofo: where would I put that code? I’m having problems with that as well…

    mae, you could put it in your theme’s functions.php file.

    yes! it worked! thanks a lot filosofo!

    How would you modify the where clause for the RSS feeds?

    i have the same question as highwind.

    ALSO.. i’ve managed to use a few ifelse statements to exclude the categories i want from the loop, but i have the same problem as the original poster in this thread.

    how can i exclude the categories but still preserve the recent post count ??

    i’m assuming i could use this code… but how would i modify it to exclude multiple categories ?

    function modify_where_clause($where) {
    global $wp_query, $wpdb;
    if ($wp_query->is_home && !strstr($where,’category_id’))
    $where .= ” AND ID NOT IN (SELECT post_id FROM $wpdb->post2cat WHERE category_id=2)”;
    return $where;
    }
    add_filter(‘posts_where’,’modify_where_clause’);

    also, i’m not sure exactly where to put that little bit of code, or how to call it. can anyone help ?

    I’m sort of wondering about the same thing. I want to INCLUDE multiple categories, or more correctly, I want to do this more than one time on the same page (as outlined here).

    Possible?

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘[Resolved] Exclude category from the loop?’ is closed to new replies.