tax_query in WP_Query problem

时间:2014-05-29 作者:roofdog

我正在尝试创建一个小部件,它可以为我的插件查询多个分类法,但我的查询有问题。

我的问题是:

    $query_args = array(
        \'numberposts\' => $limit,
        \'order\' => \'DESC\',
        \'orderby\' => \'post_date\',
        \'post_type\' => \'match\',
        \'posts_per_page\' => $limit,
    );

    $query_args[\'meta_query\'] = array(
        \'key\' => \'played\',
        \'value\' => true,
    );

    $query_args[\'meta_query\'] = array(
                    \'relation\' => \'OR\',
        array(
            \'key\' => \'home_club\',
            \'value\' => $club,
        ),
        array(
            \'key\' => \'away_club\',
            \'value\' => $club,
        ),
    );

    $query_args[\'tax_query\'] = array(
        array(
            \'taxonomy\' => \'comp\',
            \'terms\' => $comp,
            \'field\' => \'term_id\'
        ),
        array(
            \'taxonomy\' => \'season\',
            \'terms\' => $season,
            \'field\' => \'term_id\'
        ),
        array(
            \'taxonomy\' => \'team\',
            \'terms\' => $team,
            \'field\' => \'term_id\'
        ),
        array(
            \'taxonomy\' => \'venue\',
            \'terms\' => $venue,
            \'field\' => \'term_id\'
        ),
    );

    $r = new WP_Query( $query_args );

    if ( $r->have_posts() ) {

        while ( $r->have_posts()) {
        $r->the_post();
            ...
        }
    }
我99.9%确信分类法是正确注册的,因为它们在站点的许多其他区域使用,包括使用get_posts.

上面的查询没有输出任何内容,就好像没有匹配项一样。如果我添加\'relation\' => \'OR\',tax_query 它显示一个匹配列表,但没有分类过滤,如果删除tax_query 彻底地

生成以下SQL查询:

SELECT SQL_CALC_FOUND_ROWS hoc_posts.ID 
FROM hoc_posts 
INNER JOIN hoc_term_relationships 
ON (hoc_posts.ID = hoc_term_relationships.object_id) 
INNER JOIN hoc_term_relationships AS tt1 
ON (hoc_posts.ID = tt1.object_id) 
INNER JOIN hoc_term_relationships AS tt2 
ON (hoc_posts.ID = tt2.object_id) 
WHERE 1=1 
AND ( hoc_term_relationships.term_taxonomy_id IN (17) OR tt1.term_taxonomy_id IN (3) OR tt2.term_taxonomy_id IN (4) ) 
AND hoc_posts.post_type = \'match\' 
AND (hoc_posts.post_status = \'publish\' OR hoc_posts.post_status = \'private\') 
GROUP BY hoc_posts.ID 
ORDER BY hoc_posts.post_date DESC LIMIT 0, 6
以及print_r( $query_args ); 是:

Array ( 
    [numberposts] => 6 
    [order] => DESC 
    [orderby] => post_date 
    [post_type] => match 
    [posts_per_page] => 6 
    [meta_query] => Array ( 
        [key] => played 
        [value] => 1 
    ) 
    [tax_query] => Array ( 
        [relation] => OR 
        [0] => Array ( 
        [taxonomy] => comp 
        [terms] => 17 
        [field] => term_id 
        ) 
        [1] => Array ( 
        [taxonomy] => season 
        [terms] => 3 
        [field] => term_id 
        ) 
        [2] => Array ( 
       [taxonomy] => team 
        [terms] => -1 
        [field] => term_id 
        ) 
        [3] => Array ( 
        [taxonomy] => venue 
        [terms] => 4 
        [field] => term_id 
        ) 
    ) 
)
我正在竭尽全力想找出问题所在。我在这个网站上搜索了几个相关的帖子,但似乎没有一个能解决我的问题。

任何帮助都将不胜感激。:)

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

感谢Rarst为我指明了正确的方向。经过一番尝试,以下查询现在产生了我想要的效果:

$query_args = array(
    \'numberposts\' => $limit,
    \'order\' => \'DESC\',
    \'orderby\' => \'post_date\',
    \'post_type\' => \'match\',
);

$query_args[\'meta_query\'] = array(
    \'key\' => \'played\',
    \'value\' => true,
);

if ( isset( $comp ) )
    $query_args[\'tax_query\'][] = array(
        \'taxonomy\' => \'comp\',
        \'terms\' => $comp,
        \'field\' => \'term_id\',
    );

if ( isset( $season ) )
    $query_args[\'tax_query\'][] = array(
        \'taxonomy\' => \'season\',
        \'terms\' => $season,
        \'field\' => \'term_id\',
    );

if ( isset( $team ) )
    $query_args[\'tax_query\'][] = array(
        \'taxonomy\' => \'team\',
        \'terms\' => $team,
        \'field\' => \'term_id\',
    );

if ( $venue > 0  )
    $query_args[\'tax_query\'][] = array(
        \'taxonomy\' => \'venue\',
        \'terms\' => $venue,
        \'field\' => \'term_id\',
    );

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post