I would like to display two different post types in the same query… nothing strange so far. But I would like to declare what taxonomies include and what exclude for both post types, so, for instance, I would like to display posts from the category “16” but that do not belong to “19” as well, and portfolio items from taxonomy “32” that do not belong to “34” at the same time.

I thought this is the right way:

$args = array(
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'date',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => array( 16 ),
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => array( 19 ),
                'operator' => 'NOT IN'
            ),
        ),
        array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'portfolio_category',
                'field'    => 'term_id',
                'terms'    => array( 32 ),
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => 'portfolio_category',
                'field'    => 'term_id',
                'terms'    => array( 34 ),
                'operator' => 'NOT IN'
            ),
        ),
    ),
);

but it doesn’t work. Any clue on this?

Read more here: Complex WP_Query (two post types and multiple operators)


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: