Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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

share|improve this question
    
Why is that information different than what you posted below? That is, why does that query have ["terms"]=> string(3) "one" while you posted ["terms"]=> array(1) { [0]=> string(1) "1" } below in a comment to my answer? –  s_ha_dum Mar 30 '13 at 4:23
    
So Sorry! Made changes to the slugs (from '1' to 'one') and removed an array whilst waiting for your response. Winding back now, and will edit in the var_dump so we're all on the same page. Thanks for picking that up :) –  rbcpt Mar 30 '13 at 4:33
    
See my edit, please –  s_ha_dum Mar 30 '13 at 14:39
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
No dice, unfortunately. I made the change (remove []) and then ran a var_dump($tax_queries), and the output is: array(1) { [0]=> array(3) { ["taxonomy"]=> string(15) "difficulty_mode" ["terms"]=> array(1) { [0]=> string(1) "1" } ["field"]=> string(4) "slug" } } –  rbcpt Mar 30 '13 at 3:11
    
It was not obvious until you posted that, but look carefully at that var_dump. You are telling the tax query to use slugs but you are apparently feeding it IDs instead-- ["terms"]=> array(1) { [0]=> string(1) "1" } You need to make up your mind which you are going to use. –  s_ha_dum Mar 30 '13 at 3:20
    
It's definitely passing slugs - the slugs just happen to be numbers (see: d.pr/i/5RoU). Could this be the root of the issue? It's expecting alpha, but getting numeric? –  rbcpt Mar 30 '13 at 3:28
    
You have strings in the array. Those aren't actually numeric. Still, that might be causing an issue. I'd have to investigate that.var_dump your whole $query and edit it into your question. –  s_ha_dum Mar 30 '13 at 3:37
    
Thanks @s_ha_dum. var_dump output has been edited into my question. –  rbcpt Mar 30 '13 at 4:11
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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