WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Exclude category from the loop? (14 posts)

  1. Austin Matzko
    Member
    Posted 8 years ago #

    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.

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 8 years ago #

    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.

  3. Austin Matzko
    Member
    Posted 8 years ago #

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

  4. ryoken
    Member
    Posted 8 years ago #

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

  5. Austin Matzko
    Member
    Posted 8 years ago #

    How?

  6. ryoken
    Member
    Posted 8 years ago #

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

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

  7. Austin Matzko
    Member
    Posted 8 years ago #

    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.

  8. mae
    Member
    Posted 8 years ago #

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

  9. Austin Matzko
    Member
    Posted 7 years ago #

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

  10. mae
    Member
    Posted 7 years ago #

    yes! it worked! thanks a lot filosofo!

  11. highwind
    Member
    Posted 7 years ago #

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

  12. gerld
    Member
    Posted 7 years ago #

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

  13. gerld
    Member
    Posted 7 years ago #

    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 ?

  14. Haarball
    Member
    Posted 7 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic