WordPress.org

Ready to get started?Download WordPress

Forums

Query_posts : include AND exclude categories (2 posts)

  1. c13303
    Member
    Posted 4 months ago #

    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

  2. vtxyzzy
    Member
    Posted 4 months ago #

    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 );

Reply

You must log in to post.

About this Topic