使用TAX_QUERY时,结果消失(0=1生成)

时间:2013-03-30 作者:rbcpt

我正在尝试执行一个动态查询,该查询使用动态传递的分类法和post\\u类型,并相应地显示结果-

用户从表单上的3个下拉列表中进行选择,每个下拉列表都有一个值。这是通过URL查询字符串传递的。页面上的查询从URL中提取参数,利用$_GET.

当我只拉post\\u类型和其他一些片段时,这段代码工作得很好。

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

$query = new WP_Query($dat_argument);   
但是,当我包括下面的tax\\u查询时。。。

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);   
当我运行时,我得到了0个帖子和0=1的输出print_r($query).

以下是查询的var\\u转储:

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) { } }
完全被难住了。我已经阅读了所有与tax\\u query相关的线程,并尝试了我能做的任何事情,但没有成功。

Any thoughts on what\'s causing this?

参考点:

  • 这些都位于自定义函数的顶部。论文的php文件。它是一个全局函数,因此可以被另一个函数引用,该函数提取查询输出的帖子的帖子ID。

    使用“类型-自定义字段和类型的完整解决方案”创建/管理自定义帖子类型、分类法和字段。

    主题是论文1.8.4

3 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

tax_query takes an array of arrays. 您有一个数组的数组。var_dump($tax_queries); 并将得到以下结果:

array(1) {
  [0]=>
  array(1) {
    [0]=>
    array(3) {
      ["taxonomy"]=>
      string(15) "difficulty_mode"
      ["terms"]=>
      NULL
      ["field"]=>
      string(4) "slug"
    }
  }
}
尝试不使用方括号。就是这样:

$tax_queries[] = array(
    array (
    \'taxonomy\' => \'difficulty_mode\',
    \'terms\' => $mode,
    \'field\' => \'slug\'
    )
);
进入这个

$tax_queries = array(
    array (
    \'taxonomy\' => \'difficulty_mode\',
    \'terms\' => $mode,
    \'field\' => \'slug\'
    )
);
编辑:我已经做了一些额外的测试。当我在测试服务器上编辑此查询以使其具有已知的良好值时,它就会工作。以下查询完全按照预期工作:

$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);
如果我将其编辑为具有已知的错误值,例如--\'terms\' => array(\'nada\'),\'taxonomy\' => \'bad_category\',-- 我得到了1=1 AND 1 = 0 导致查询不返回任何内容的查询段。问题所在is not the query. 问题是错误的值被传递到查询中。

我不知道您的哪些值是错误的,但要么您的分类名称是错误的,要么您的slug是错误的。没有其他选择。使用没有关联帖子的有效slug只返回一个空结果集,但没有0 = 1 组成部分

SO网友:buzibuzi

使用一维数组。

更改此

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

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

SO网友:Shawn DeWolfe

我所做的修复是加入一个过滤器来处理0=1的问题:在我的情况下,我试图只影响产品帖子类型,所以我不想过滤和影响所有SQL语句。

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;
}

结束