8

简单查询但由于某种原因未显示正确的帖子,尝试显示带有每月待办事项列表术语的帖子,如果没有结果则显示带有社区活动术语的帖子。有什么建议么?

$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'
);
4

1 回答 1

13

尝试添加数组并使其如下所示:

$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'
);

您可能会注意到术语是复数,因此您也可以像这样简化查询:

$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'
);
于 2017-10-02T19:43:12.190 回答