Title: Misbehahiour in get_posts&#8217; category exclude behaviour?
Last modified: August 19, 2016

---

# Misbehahiour in get_posts’ category exclude behaviour?

 *  [wpseek](https://wordpress.org/support/users/alphawolf/)
 * (@alphawolf)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/misbehahiour-in-get_posts/)
 * 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)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/misbehahiour-in-get_posts/#post-879360)
 * 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](http://codex.wordpress.org/index.php?title=Template_Tags%2Fquery_posts&diff=62497&oldid=62195)
 *  Thread Starter [wpseek](https://wordpress.org/support/users/alphawolf/)
 * (@alphawolf)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/misbehahiour-in-get_posts/#post-879364)
 * Thanks for your quick response and clarification, MichaelH. Sadly.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/misbehahiour-in-get_posts/#post-879368)
 * Does the **category_and** parameter, in any way, mitigate your sadness?
 *  Thread Starter [wpseek](https://wordpress.org/support/users/alphawolf/)
 * (@alphawolf)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/misbehahiour-in-get_posts/#post-879426)
 * 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.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/misbehahiour-in-get_posts/#post-879439)
 * 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.
 *  Thread Starter [wpseek](https://wordpress.org/support/users/alphawolf/)
 * (@alphawolf)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/misbehahiour-in-get_posts/#post-879465)
 * 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. 🙂
 *  [macarrier_hue](https://wordpress.org/support/users/macarrier_hue/)
 * (@macarrier_hue)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/misbehahiour-in-get_posts/#post-879621)
 * 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.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [exclude](https://wordpress.org/support/topic-tag/exclude/)
 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * 7 replies
 * 3 participants
 * Last reply from: [macarrier_hue](https://wordpress.org/support/users/macarrier_hue/)
 * Last activity: [16 years, 7 months ago](https://wordpress.org/support/topic/misbehahiour-in-get_posts/#post-879621)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
