WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] exclude_category Doesn't work with WordPress 3.1 (26 posts)

  1. Isolacious
    Member
    Posted 3 years ago #

    Used the code:

    function exclude_category($query) {
    if ( $query->is_home ) {
    $query->set('cat', '-147');
    }
    return $query;
    }
    add_filter('pre_get_posts', 'exclude_category');

    to exclude posts from a category from showing up on the front page, but since I updated WordPress to 3.1 it doesn't work anymore.

    EDIT: Also noticed that the URL of categories has changed from "http://www.example.com/?cat=xx" to "http://www.example.com/?category_name=NAMEOFCAT"

    How do I fix this?

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    not sure if its related, are you using the 'Simple Tags' plugin?

    Its gone haywire for me on 3.1, and ruined most of my category related queries

  3. Isolacious
    Member
    Posted 3 years ago #

    No, not using 'Simple Tags'. Hasn't really gone haywire just yet, but nonetheless I'm regretting clicking the "Automatically Update" button.

  4. MK Safi
    Member
    Posted 3 years ago #

    I have this problem too. I didn't expect things to break -- but I also regret not upgrading the test version of my website first before upgrading production.

    Anyway, a solution to this problem would help a lot. Thanks!

  5. greenflag
    Member
    Posted 3 years ago #

    I have the same issue. lot's of posts showing up on main page that shouldn't be there.

  6. Isolacious
    Member
    Posted 3 years ago #

    @greenflag: Yeah been messing around trying to get it to work but nothing so far. Do tell me if you find something, would really help.

  7. greenflag
    Member
    Posted 3 years ago #

    nothing on my end. i searched for answers and this is so new there is no info on it yet...

  8. greenflag
    Member
    Posted 3 years ago #

    i do have other edits (like limit # of posts on main page) that are working just fine. it only seems to be this one...

  9. Isolacious
    Member
    Posted 3 years ago #

    @greenflag: Yeah all of that works, in fact the "Functions.php" was not overwritten in the update (I had it write protected before hand). It's just the "exclude_category" function that doesn't work right.

    Did you notice the change in the URL format (For Categories) as well?

  10. greenflag
    Member
    Posted 3 years ago #

    i didn't notice categories change, no. i use a plugin that removes /category/ from the url.

  11. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

  12. Isolacious
    Member
    Posted 3 years ago #

    Too bad, meaning for now there's no way to fix this yeah?
    Well, guess I could survive for a couple of days.

  13. Samuel Wood (Otto)
    Tech Ninja
    Posted 3 years ago #

    Try using category__not_in instead of cat.

    function exclude_category($query) {
    if ( $query->is_home ) {
    $query->set('category__not_in', '147');
    }
    return $query;
    }
    add_filter('pre_get_posts', 'exclude_category');
  14. Isolacious
    Member
    Posted 3 years ago #

    @Otto; Thanks, that actually works. So why exactly does this work?

  15. Samuel Wood (Otto)
    Tech Ninja
    Posted 3 years ago #

    Because of a bug in the "cat" parameter that is not in the "category__not_in" parameter.

  16. lestermc
    Member
    Posted 3 years ago #

    But does this work for various categories or just one?

  17. Isolacious
    Member
    Posted 3 years ago #

    I'm using it for just one category, but I don't see why it wouldn't work for many. Just separate the category numbers by a space.

  18. lestermc
    Member
    Posted 3 years ago #

    i try it for like 7 categories and doesn't work but :(

  19. Isolacious
    Member
    Posted 3 years ago #

    Unless you're using the latest version of WordPress try putting a - before the category number

  20. greenflag
    Member
    Posted 3 years ago #

    Yes i found the same thing - it only works with 1 category.

  21. Isolacious
    Member
    Posted 3 years ago #

    So I guess you're using an older version of WordPress?, I'm currently using 3.1 and I don't need the -.

  22. greenflag
    Member
    Posted 3 years ago #

    i'm using 3.1. the way of doing it mentioned in 1st post here allowed for multiple cat exclusion like this:

    -1 -2 -3 -4 etc

    the way otto suggests seems to work for only for 1 category - meaning:

    1 2 3 4 wont work

  23. lestermc
    Member
    Posted 3 years ago #

    Yeah it only works for one category

  24. lestermc
    Member
    Posted 3 years ago #

    This plugin is working!!! click here

  25. mmorpglife
    Member
    Posted 3 years ago #

    If you want to fix it for multiple categories on your own without using a plugin you can change your exclude function to something like this:

    function exclude_category($query) {
    $exclude_categories=array('-1','-2','-3','-4');
    if ( $query->is_home ) {
    $query->set('category__not_in', $exclude_categories);
    }
    return $query;
    }
    add_filter('pre_get_posts', 'exclude_category');

    NOTE: use your own category number in the $exclude_categories array.
    NOTE 2: I've noticed that using positive numbers for categories works just as well.

  26. somethingelse
    Member
    Posted 3 years ago #

    BRILLIANT FIX

    function exclude_category($query) {
    if ( $query->is_home ) {
    $query->set('category__not_in', '147');
    }
    return $query;
    }
    add_filter('pre_get_posts', 'exclude_category'):

    Thanks @otto! I wasted about 3 hours trying things this morning before it occurred to me this might be a 3.1 bug... and your function totally fixed my problem in 5 seconds.

    have made a note, too, @mmorpglife of your multi-category fix.

    thankful there are people like you who can think in code, cuz i surely cannot. :D

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.