WP_Query多个帖子类型和分类法

问题描述 投票:4回答:1

我想从两种帖子类型中获得结果,

1)发布,仅显示“业务”提示2)本地新闻,显示所有跳线

我到目前为止:

$args=array(
'cat' => $my_category_id,
'post_status' => 'publish',
'posts_per_page' => 5,
'post_type' => array('post', 'local-news'),
'tax_query' => array(
      'relation' => 'OR',
   array(
     'taxonomy' => 'postkicker',
      'term' => 'business'
   ),
    array(
     'taxonomy' => 'impactkicker',
   ),
 ),
'orderby'    => 'date',
'order'      => 'DESC'
);

目前不同时显示这两种信息,有没有建议?在此先感谢

wordpress post custom-post-type taxonomy
1个回答
0
投票

您还需要确保已将cpt local-news连接到category分类标准,因为您正试图通过此分类标准进行选择。这是广泛评论的方法,它将满足您的需求。至少如果我清楚地知道你的想法。如果还没有设置类别,则可以将tax_query主关系更改为OR而不是AND以输出项目。]

$args = array(
    // 'cat' => $my_category_id, // better  replace it with category in tax_query, see below.
    'post_status'    => 'publish',
    'posts_per_page' => 5,
    'post_type'      => array( 'post', 'local-news' ),
    'tax_query'      => array(
        'relation' => 'AND', // we set it to AND because we want all posts of this category i guess.
        array(
            'taxonomy' => 'category',
            'term'     => $my_category_id,
            'field'    => 'term_id',
            'operator' => 'IN', // just to be more explicit.
        ),
        array( // we create nested sub queries which will filter by other 2 taxonomies, which in turn has OR relation.
            'relation' => 'OR',
            array(
                'taxonomy' => 'postkicker',
                'field'    => 'slug',    // by default it's term_id and you are passing in a slug so set it explicitly.
                'term'     => 'business',
                'operator' => 'IN', // just to be more explicit.
            ),
            array(
                'taxonomy' => 'impactkicker',
                'field'    => 'slug',           // set these or not add rule for taxonomy at all.
                'term'     => 'your-term-slug', // same here.
                'operator' => 'IN', // it's a default value, but ou can set 'EXISTS' if you need to check if whatever term of such taxonomy is assigned.
            ),
        ),
    ),
    'orderby'        => 'date',
    'order'          => 'DESC',
);
© www.soinside.com 2019 - 2024. All rights reserved.