1

I'm trying to create a nested tax query that does the following:

  1. Exclude posts that have any of several specified category IDs.
  2. AND include posts that have any of several other specified category IDs, OR posts that have any of several specified tag IDs.

Here's what I've tried:

$args['tax_query'] = array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'category',
        'field'    => 'term_id',
        'terms'    => $omit_category_ids,
        'operator' => 'NOT IN',
    ),
    array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => $category_ids,
            'operator' => 'IN',
        ),
        array(
            'taxonomy' => 'post_tag',
            'field'    => 'term_id',
            'terms'    => $term_ids,
            'operator' => 'IN',
        ),
    ),
);

This doesn't limit posts to only the categories I want, and it also sometimes has posts in the categories I want to omit.

When I remove the nesting, and instead use this:

$args['tax_query'] = array(
    'relation' => 'OR',
    array(
        'taxonomy' => 'category',
        'field'    => 'term_id',
        'terms'    => $category_ids,
    ),
    array(
        'taxonomy' => 'post_tag',
        'field'    => 'term_id',
        'terms'    => $term_ids,
    ),
);

It works, but of course it does not exclude the categories I need to exclude.

Is what I need to achieve possible with WP_Query, or do I need a filter?

1 Answer 1

1

I think I figured it out. This query appears to work:

$args = array();
if ( isset( $term_ids ) && isset( $category_ids ) ) {
    $args['tax_query'] = array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => $omit_category_ids,
            'operator' => 'NOT IN',
        ),
        array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => $category_ids,
                'operator' => 'IN',
            ),
            array(
                'taxonomy' => 'post_tag',
                'field'    => 'term_id',
                'terms'    => $term_ids,
                'operator' => 'IN',
            ),
        ),
    );
} elseif ( isset( $term_ids ) ) {
    $args['tax_query'] = array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => $omit_category_ids,
            'operator' => 'NOT IN',
        ),
        array(
            'taxonomy' => 'post_tag',
            'field'    => 'term_id',
            'terms'    => $term_ids,
            'operator' => 'IN',
        ),
    );
} elseif ( isset( $category_ids ) ) {
    $args['tax_query'] = array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => $omit_category_ids,
            'operator' => 'NOT IN',
        ),
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => $category_ids,
            'operator' => 'IN',
        ),
    );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.