When tax_query used, results disappear (0 =1 produced)

tax_query takes an array of arrays. You have an array of arrays of arrays. var_dump($tax_queries); and will get this:

array(1) {
  [0]=>
  array(1) {
    [0]=>
    array(3) {
      ["taxonomy"]=>
      string(15) "difficulty_mode"
      ["terms"]=>
      NULL
      ["field"]=>
      string(4) "slug"
    }
  }
}

Try it without the square brackets. That is turn this:

$tax_queries[] = array(
    array (
    'taxonomy' => 'difficulty_mode',
    'terms' => $mode,
    'field' => 'slug'
    )
);

into this

$tax_queries = array(
    array (
    'taxonomy' => 'difficulty_mode',
    'terms' => $mode,
    'field' => 'slug'
    )
);

Edit:

I have done some additional testing. When I edit this query to have known good values on my test server, it works. The following query works exactly as expected:

$tax_queries = array(
    array (
    'taxonomy' => 'category',
    'terms' => array('aciform'),
    'field' => 'slug'
    )
);

$dat_argument = array ( 'post_type' => 'post',
                        'orderby' => 'rand',
                        'showposts' => 10,
                        'tax_query' => $tax_queries
                        );

$query = new WP_Query($dat_argument);  
var_dump($query);

If I edit it to have a known bad value– for example– 'terms' => array('nada'), or 'taxonomy' => 'bad_category',— I get the 1=1 AND 1 = 0 segment of query which caused the query to return nothing. The problem is not the query. The problem is that bad values are being passed into the query.

I don’t know which of your values is wrong but either your taxonomy name is wrong, or your slug(s) are wrong. There isn’t any other option. Using valid slugs with no associated posts just return an empty result set but don’t have the 0 = 1 component.

Leave a Comment

tech