wordpress - WP_Query 多个帖子类型和分类法

标签 wordpress post custom-post-type taxonomy

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

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

目前没有显示两种帖子类型,有什么建议吗?提前致谢

最佳答案

你需要确保你已经连接了你的cpt local-newscategory分类法也是如此,因为您正在尝试按此分类法进行选择。这是广泛评论的方法,我猜它可以满足您的需求。至少如果我清楚地了解你的想法。您可以更改 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',
);

关于wordpress - WP_Query 多个帖子类型和分类法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39780107/

相关文章:

php - get_the_id 与 post->ID 与 the_id/get_post_meta

mysql - 如果没有事务,wordpress 是如何工作的呢?

php - 本地 WordPress 站点无法访问本地数据库(不是建立连接时出错)

wordpress - RGFormsModel 问题重力形式

json - 如何在 Laravel 4 中将 JSON 数据 POST 到远程服务器?

delphi - 如何在带有 INDY 的 Delphi 10.3 中使用 HTTPS

php - 具有自定义字段类别的下一个/上一个自定义帖子类型

javascript - 使用 JSON POST 请求

php - 使用自定义元日期显示自定义帖子类型,但保留 WP 中的日程安排功能

wordpress - 在自定义帖子类型内的自定义分类上用下拉选择器替换层次结构复选框