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

I’m trying to perform a dynamic query that uses a dynamically passed taxonomy and post_type, and displays results accordingly —

A user makes a selection from 3 drop-downs on a form, each have a value. This is passed by the URL Query string. The query on page picks up parameters from the URL, utilising $_GET.

Read More

When I just pull the post_type and a few other pieces, this code works fine.

global $query
$dat_argument = array ( 'post_type' => $type,
                        'orderby' => 'rand',
                        'showposts' => $posts,
                        );

$query = new WP_Query($dat_argument);   

However, when I include the tax_query as below…

global $query;

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

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

$query = new WP_Query($dat_argument);   

I get 0 posts and an output of 0 = 1 when I run print_r($query).

Below is the var_dump of the query:

object(WP_Query)#203 (44) { 
["query_vars"]=> array(61) { 
["post_type"]=> string(11) "bw-exercise" 
["orderby"]=> string(4) "rand"
["showposts"]=> int(5)
["tax_query"]=> array(1) {
[0]=> array(3) {
["taxonomy"]=> string(15) "difficulty_mode"
["terms"]=> array(1) {
[0]=> string(1) "1" }
["field"]=> string(4) "slug" } }
["error"]=> string(0) ""
["m"]=> int(0)
["p"]=> int(0)
["post_parent"]=> string(0) ""
["subpost"]=> string(0) ""
["subpost_id"]=> string(0) ""
["attachment"]=> string(0) ""
["attachment_id"]=> int(0)
["name"]=> string(0) ""
["static"]=> string(0) ""
["pagename"]=> string(0) ""
["page_id"]=> int(0)
["second"]=> string(0) ""
["minute"]=> string(0) ""
["hour"]=> string(0) ""
["day"]=> int(0)
["monthnum"]=> int(0)
["year"]=> int(0)
["w"]=> int(0)
["category_name"]=> string(0) ""
["tag"]=> string(0) ""
["cat"]=> string(0) ""
["tag_id"]=> string(0) ""
["author_name"]=> string(0) ""
["feed"]=> string(0) ""
["tb"]=> string(0) ""
["paged"]=> int(0)
["comments_popup"]=> string(0) ""
["meta_key"]=> string(0) ""
["meta_value"]=> string(0) ""
["preview"]=> string(0) ""
["s"]=> string(0) ""
["sentence"]=> string(0) ""
["fields"]=> string(0) ""
["menu_order"]=> string(0) ""
["category__in"]=> array(0) { }
["category__not_in"]=> array(0) { }
["category__and"]=> array(0) { }
["post__in"]=> array(0) { }
["post__not_in"]=> array(0) { }
["tag__in"]=> array(0) { }
["tag__not_in"]=> array(0) { }
["tag__and"]=> array(0) { }
["tag_slug__in"]=> array(0) { }
["tag_slug__and"]=> array(0) { }
["ignore_sticky_posts"]=> bool(false)
["suppress_filters"]=> bool(false)
["cache_results"]=> bool(true)
["update_post_term_cache"]=> bool(true)
["update_post_meta_cache"]=> bool(true)
["posts_per_page"]=> int(5)
["nopaging"]=> bool(false)
["comments_per_page"]=> string(2) "50"
["no_found_rows"]=> bool(false)
["taxonomy"]=> string(15) "difficulty_mode"
["term"]=> string(1) "1"
["order"]=> string(4) "DESC" }
["tax_query"]=> object(WP_Tax_Query)#200 (2) {
["queries"]=> array(1) {
[0]=> array(5) {
["taxonomy"]=> string(15) "difficulty_mode"
["terms"]=> array(1) {
[0]=> string(1) "1" }
["include_children"]=> bool(true)
["field"]=> string(4) "slug"
["operator"]=> string(2) "IN" } }
["relation"]=> string(3) "AND" }
["meta_query"]=> object(WP_Meta_Query)#201 (2) {
["queries"]=> array(0) { }
["relation"]=> NULL }
["post_count"]=> int(0)
["current_post"]=> int(-1)
["in_the_loop"]=> bool(false)
["comment_count"]=> int(0)
["current_comment"]=> int(-1)
["found_posts"]=> int(0)
["max_num_pages"]=> int(0)
["max_num_comment_pages"]=> int(0)
["is_single"]=> bool(false)
["is_preview"]=> bool(false)
["is_page"]=> bool(false)
["is_archive"]=> bool(true)
["is_date"]=> bool(false)
["is_year"]=> bool(false)
["is_month"]=> bool(false)
["is_day"]=> bool(false)
["is_time"]=> bool(false)
["is_author"]=> bool(false)
["is_category"]=> bool(false)
["is_tag"]=> bool(false)
["is_tax"]=> bool(true)
["is_search"]=> bool(false)
["is_feed"]=> bool(false)
["is_comment_feed"]=> bool(false)
["is_trackback"]=> bool(false)
["is_home"]=> bool(false)
["is_404"]=> bool(false)
["is_comments_popup"]=> bool(false)
["is_paged"]=> bool(false)
["is_admin"]=> bool(false)
["is_attachment"]=> bool(false)
["is_singular"]=> bool(false)
["is_robots"]=> bool(false)
["is_posts_page"]=> bool(false)
["is_post_type_archive"]=> bool(false)
["query_vars_hash"]=> string(32) "d289f5774bbde27aa3902982141a66ab"
["query_vars_changed"]=> bool(false)
["thumbnails_cached"]=> bool(false)
["query"]=> array(4) {
["post_type"]=> string(11) "bw-exercise"
["orderby"]=> string(4) "rand"
["showposts"]=> string(1) "5"
["tax_query"]=> array(1) {
[0]=> array(3) {
["taxonomy"]=> string(15) "difficulty_mode"
["terms"]=> array(1) {
[0]=> string(1) "1" }
["field"]=> string(4) "slug" } } }
["request"]=> string(278) "SELECT SQL_CALC_FOUND_ROWS wrd_posts.ID FROM wrd_posts WHERE 1=1 AND 0 = 1 AND wrd_posts.post_type = 'bw-exercise' AND (wrd_posts.post_status = 'publish' OR wrd_posts.post_author = 1 AND wrd_posts.post_status = 'private') GROUP BY wrd_posts.ID ORDER BY RAND() DESC LIMIT 0, 5"
["posts"]=> array(0) { } }

Absolutely stumped. I’ve read up on all the threads to do with tax_query and tried anything I can, but no dice.

Any thoughts on what’s causing this?

Reference Points:

  • This is all held at the top of the custom functions.php file for Thesis. It’s a global so that it can be referred to by another function that pulls the post ID of the posts that are output from the query.

  • Using ‘Types – Complete Solution for Custom Fields and Types’ to create/manage custom post types, taxonomies and fields.

  • Theme is Thesis 1.8.4

Related posts

Leave a Reply

4 comments

  1. 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.

  2. Use one dimension array.

    Change this

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

    to

    'tax_query' => array(
        'taxonomy' => 'difficulty_mode',
        'terms' => $mode,
        'field' => 'slug'
    );
    
  3. The fix I did was to put in a filter to deal with the 0 = 1 issue:
    In my case, I was trying to only affect the product post type, so I didn’t want to filter and affect all SQL statements.

    add_filter( 'posts_where', 'acff_sqlfix', 10, 1 );
    
    function acff_sqlfix( $sql ) {
            if ((get_query_var( 'post_type' ) == 'product') && (strpos($sql, ' 0 = 1') > 0)) {
                $sql = str_replace(' 0 = 1', ' 1 = 1', $sql);
            }
            return $sql;
    }
    
  4. In your tax_query, try setting field to term_taxonomy_id and using $term->term_taxonomy_id:

            $args = [
                'post_type' => 'post',
                'post_status' => 'publish',
                'tax_query' => [
                    [
                        'taxonomy' => 'my_custom_taxonomy',
                        'field' => 'term_taxonomy_id',
                        'terms' => $term->term_taxonomy_id,
                    ]
                ]
            ];
            $query = new WP_Query($args);