我正在尝试创建一个小部件,它可以为我的插件查询多个分类法,但我的查询有问题。
我的问题是:
$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
)
)
)
我正在竭尽全力想找出问题所在。我在这个网站上搜索了几个相关的帖子,但似乎没有一个能解决我的问题。
任何帮助都将不胜感激。:)
最合适的回答,由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\',
);