WordPress.org

Ready to get started?Download WordPress

Forums

Misbehahiour in get_posts' category exclude behaviour? (8 posts)

  1. Alphawolf
    Member
    Posted 5 years ago #

    Hey folks,

    I am about to recode my little Archive plugin, and so changed the way to exclude categories in my code. I am using get_posts and the category parameter to exclude certain cat ids (category=-1,-2,-3...).

    Now, the Codex sais the following to query_posts:

    There is a proviso however: it will exclude all the posts that belong only to category xxx. If a post belongs to another category as well, it will still be picked up.

    Furthermore, the Codex tells that since WP 2.6 query_posts and get_posts run the same DB query internally, so the quoted should also be working for get_posts. Theoretically.

    But obviously thats not a given. If a post belongs to another category as well, it's not gonna be picked up.

    Now, is this a misbehaviour in WP, or is the Codex just not telling the fact?

    Regards,
    Oliver

  2. MichaelH
    Member
    Posted 5 years ago #

    I'm thinking the Codex is wrong about that. I used <?php query_posts('cat=-17'); ?> and it excluded ALL posts with category 17, even when the post belonged to multiple categories.

    I will fix that in Codex.
    http://codex.wordpress.org/index.php?title=Template_Tags%2Fquery_posts&diff=62497&oldid=62195

  3. Alphawolf
    Member
    Posted 5 years ago #

    Thanks for your quick response and clarification, MichaelH. Sadly.

  4. MichaelH
    Member
    Posted 5 years ago #

    Does the category_and parameter, in any way, mitigate your sadness?

  5. Alphawolf
    Member
    Posted 5 years ago #

    Well, sadly no. In my case I guess I'd have to use
    'category__not_in' => array(3)
    to exclude posts that only belong to category 3. Still it excluded ALL posts with category 3, even when the post belonged to multiple categories.

  6. MichaelH
    Member
    Posted 5 years ago #

    That leaves you with some ugly choices doesn't it?

    Guess you'll have to have a query of all posts, then use something like get_categories and test that for posts that just have one category.

  7. Alphawolf
    Member
    Posted 5 years ago #

    Well, I do have some workarounds in mind, but those are either performance-killers or a plus of dozens of extra code lines or both.

    But thanks anyways for the clarification on this issue. :-)

  8. macarrier_hue
    Member
    Posted 4 years ago #

    i have some issues with get_posts($args), $args beeing array('category' => 176) to include all posts from category ID 176.

    apparently, get_posts won't pick up posts that are children to this category if they're also child to another. any workaround?

Topic Closed

This topic has been closed to new replies.

About this Topic