Title: Query_posts : include AND exclude categories
Last modified: August 21, 2016

---

# Query_posts : include AND exclude categories

 *  [c13303](https://wordpress.org/support/users/c13303/)
 * (@c13303)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/query_posts-include-and-exclude-categories/)
 * Hi, I’ve been using WP for many years, but I still have problems with a very 
   simple feature :
 * I want all posts that are in category 7537 but that aren’t in category 4.
 * According to the codex and most of the web, I got this :
 * $args = array(
    “category__in” => 7537, ‘category__not_in’ => 4, ‘posts_per_page’
   =>10 );
 * $posts_array = query_posts( $args );
 * Returning 0 results, but there are some posts realizing these conditions (a lot)
   in the database. What’s the good way to get it ?
 * Thanks a lot

Viewing 1 replies (of 1 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/query_posts-include-and-exclude-categories/#post-4761260)
 * This code (untested) should be close to what you want:
 *     ```
       $args = array(
       	'post_type' => 'post',
       	'tax_query' => array(
       		'relation' => 'AND',
       		array(
       			'taxonomy' => 'category',
       			'field' => 'id',
       			'terms' => array( 7537 )
       		),
       		array(
       			'taxonomy' => 'category',
       			'field' => 'id',
       			'terms' => array( 4 ),
       			'operator' => 'NOT IN'
       		)
       	)
       );
       $query = new WP_Query( $args );
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Query_posts : include AND exclude categories’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [12 years, 1 month ago](https://wordpress.org/support/topic/query_posts-include-and-exclude-categories/#post-4761260)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
