【问题标题】:Wordpress: tax_query multiple terms using OR operatorWordpress:tax_query 使用 OR 运算符的多个术语
【发布时间】: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'
);

【问题讨论】:

    标签: wordpress taxonomy-terms


    【解决方案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'
    );
    

    【讨论】:

      猜你喜欢
      • 2021-06-20
      • 1970-01-01
      • 2012-12-30
      • 2020-09-24
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多