Support » Plugins and Hacks » Misbehahiour in get_posts’ category exclude behaviour?

  • 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

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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

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

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

    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.

    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.

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

    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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Misbehahiour in get_posts’ category exclude behaviour?’ is closed to new replies.