I have this WP_Query:

                new WP_Query(
                            'post_type' => 'product_variation',
                            'post_status'       => 'publish',
                            'posts_per_page'    => -1,
                            'tax_query' => array(
                                    'taxonomy' => 'product_cat',
                                    'field'    => 'slug',
                                    'terms'    => array( 'archive', 'membership' ),
                                    'operator' => 'NOT IN',
                            'fields' => 'ids',

The problem is, that the product_cat taxonomy is set on the parent products, not the variations. Meaning, I need to get all the variations whose parents are not in the specified categories, or in other words: get child posts but run the tax query on the parent posts.

I know this can be achieved with MySQL, but I was wondering if there's a clean way of doing it with WP_Query.


1 Answer 1


My solution to this ended up splitting this into 2 queries:

  1. Get the IDs of all the parent products I want to ignore
  2. Get all the variations that don't a post_parent from that list of IDs

    $archived_products = wc_get_products(
            'type' => array( 'variable', 'variable-subscription' ),
            'paginate' => false,
            'limit' => -1,
            'category' => array( 'archive', 'membership' ),
            'return' => 'ids',
    $my_query->set( 'post_parent__not_in', $archived_products );

Your Answer

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

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