0

I am creating a custom filter for WooCommerce products. I have the following WP_Query to filter on selected products:

$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'tax_query' => array(
    'relation' => 'AND',
     array(
      'taxonomy' => 'pa_size',
      'terms' => $sizeFilter,
      'field' => 'slug'
     ),
     array(
      'taxonomy' => 'product_cat',
      'terms' => $catFilter,
      'field' => 'slug'
    ),
    array(
      'taxonomy' => 'pa_color',
      'terms' => $colorFilter,
      'field' => 'slug'
      )
    )
  );

I also want to add a meta_query to filter on product price. However, I want to find products with a given price AND the tax_query filter above. Is there a way to do a relation => 'AND' between the meta_query and tax_query?

'meta_query' => array(
  'relation'  => 'OR',
  array(
      'key' => '_price',
      'value' => $priceFilter,
      'compare' => 'IN'
  ),
)

When I add the meta_query within the array, no products are returned. I tried separating the two queries into two arrays and then merging the arrays, but it was not a "relation => AND".

ellier7
  • 417
  • 4
  • 9
  • 23
  • https://stackoverflow.com/questions/43734845/how-to-give-or-relation-in-tax-query-and-meta-query-wordpress#:~:text=There%20is%20no%20way%20to,of%20both%20of%20these%20queries. – mujuonly May 22 '21 at 04:48
  • Do you also have variable products ? It won't work in that case, because the prices for variation products are saved differently. I was able to solve this using Relvanssi(https://www.relevanssi.com/) plugins `relevanssi_hits_filter` hook : https://www.relevanssi.com/user-manual/filter-hooks/relevanssi_hits_filter/ – bhanu May 22 '21 at 07:07

0 Answers0