Wordpress:tax_query 使用 OR 运算符的多个术语
Posted
技术标签:
【中文标题】Wordpress:tax_query 使用 OR 运算符的多个术语【英文标题】:Wordpress: tax_query multiple terms using OR operator 【发布时间】:2018-03-13 22:00:34 【问题描述】:简单查询,但由于某种原因未显示正确的帖子,尝试显示带有 monthly-to-do-list 术语的帖子,如果没有结果,则显示与社区的帖子 -事件的术语。有什么建议吗?
$todo_args = array(
'cat' => $my_category_id,
'posts_per_page' => 1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'postkicker',
'field' => 'slug',
'terms' => 'monthly-to-do-list',
),
array(
'taxonomy' => 'postkicker',
'field' => 'slug',
'terms' => 'community-events',
),
),
'orderby' => 'date',
'order' => 'DESC'
);
【问题讨论】:
【参考方案1】:尝试添加数组并使其如下所示:
$todo_args = array(
'cat' => $my_category_id,
'posts_per_page' => 1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'postkicker',
'field' => 'slug',
'terms' => array('monthly-to-do-list'),
),
array(
'taxonomy' => 'postkicker',
'field' => 'slug',
'terms' => array('community-events'),
),
),
'orderby' => 'date',
'order' => 'DESC'
);
您可能会注意到 terms 是复数,因此您也可以像这样简化查询:
$todo_args = array(
'cat' => $my_category_id,
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'postkicker',
'field' => 'slug',
'terms' => array('monthly-to-do-list','community-events'),
),
),
'orderby' => 'date',
'order' => 'DESC'
);
【讨论】:
以上是关于Wordpress:tax_query 使用 OR 运算符的多个术语的主要内容,如果未能解决你的问题,请参考以下文章