0

Using the following code, I expect $items to not include products with the product category "stones." However, that is not the result. It doesn't seem to be excluding that category at all. Syntax looks right - are there other reasons why WordPress wouldn't respect this "NOT IN" statement?

$tax_query = array(
    array(
        'taxonomy'         => 'product_cat',
        'terms'            => array('stones'),
        'field'            => 'slug',
        'operator'         => 'NOT IN',
    ),
);
$args = array(
            'post_type' => 'product',
            'posts_per_page' => -1,
            'tax_query' => $tax_query
        );
$items = new WP_Query( $args );
2
  • 1
    If you're sure the data (e.g. slug) are good, try suppressing filters for a while (add 'suppress_filters' => true to your $args array) and just check if the category is now being excluded. You could also do echo $items->request; and see if the SQL command is good.
    – Sally CJ
    Commented Apr 6, 2022 at 23:46
  • 1
    @SallyCJ Thank you, your suggestion to echo the request to check on SQL command led me to the realization that what I was actually seeing was a server error - it was failing to upload the edited file. Wasted two hours of my life on that!
    – the.s.brom
    Commented Apr 7, 2022 at 18:13

0

Your Answer

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