Wordpress custom tax query with multiple conditions not working

I have this weird functionality that is not working in wordpress.

I'm using woocomerce and I have to get some products based on some filters using the following dump code:

  ["tax_query"]=>
  array(2) {
    [0]=>
    array(4) {
      ["taxonomy"]=>
      string(11) "product_cat"
      ["field"]=>
      string(4) "slug"
      ["terms"]=>
      array(4) {
        [0]=>
        string(3) "running"
        [1]=>
        string(9) "hiking"
      }
      ["operator"]=>
      string(3) "AND"
    }
    [1]=>
    array(4) {
      ["taxonomy"]=>
      string(11) "product_cat"
      ["field"]=>
      string(4) "slug"
      ["terms"]=>
      array(2) {
        [0]=>
        string(5) "nike"
        [1]=>
        string(9) "adidas"
      }
      ["operator"]=>
      string(2) "OR"
    }
  }

For $args['tax_query'][0] I need to make AND logic. This means that the results must decrease if I select more checkboxes.

For $args['tax_query'][1] I need to make an OR logic.

The explanation for the above code is: Give me all the products that are for running but also for hiking and is produced by nike or adidas.

The following code returns 0 results. If I remove the ["operator"]=> 'AND' from $args['tax_query'][0] it will return all the results like it just pass the filter.

The final code is:

array(6) {
  ["posts_per_page"]=>
  string(2) "12"
  ["post_status"]=>
  string(7) "publish"
  ["post_type"]=>
  string(7) "product"
  ["offset"]=>
  string(1) "0"
  ["tax_query"]=>
  array(2) {
    [0]=>
    array(4) {
      ["taxonomy"]=>
      string(11) "product_cat"
      ["field"]=>
      string(4) "slug"
      ["terms"]=>
      array(3) {
        [0]=>
        string(3) "running"
        [1]=>
        string(9) "hiking"
      }
      ["operator"]=>
      string(3) "AND"
    }
    [1]=>
    array(4) {
      ["taxonomy"]=>
      string(11) "product_cat"
      ["field"]=>
      string(4) "slug"
      ["terms"]=>
      array(2) {
        [0]=>
        string(11) "nike"
        [1]=>
        string(34) "adidas"
      }
      ["operator"]=>
      string(2) "OR"
    }
  }
  ["meta_query"]=>
  NULL
}

Any explanation or a fix for this?

Thank you.

1 answer

  • answered 2018-04-17 05:43 Noman

    You would need to use OR relation first, then AND

    Modify the arguments as you need, this solution is just an idea.

    Tested with POSTS

    // WP_Query arguments
    $args = array(
        'post_type'              => array( 'post' ),
        'post_status'            => array( 'publish' ),
        'tax_query'              => array(
            'relation' => 'OR',
            array(
                'taxonomy'         => 'category',
                'terms'            => array( 'Apple', 'Design' ),
                'field'            => 'name',
                'operator'         => 'AND',
            ),
            array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'category',
                    'terms' => array('Photography', '   Android'),
                    'field' => 'name',
                ),
            ),
        ),
    );
    
    // The Query
    $query = new WP_Query( $args );
    
    foreach ($query->posts as $key => $post) {
        echo '<pre>';print_r($post->post_title . ' - '. get_the_category_list(','));echo '</pre>';
    }