get_posts不返回多个帖子类型

时间:2018-06-08 20:43:38

标签: wordpress

我正在使用 get_posts($ query)列出我的WordPress插件中的帖子。

如果我这样使用它:

$query['post_type'] = array('post', 'attachment');

然后它不会返回'附件'类型,只有帖子。只有当我单独查询它们时才会返回它们,如下所示:

$query['post_type'] = 'attachment';

我正在使用WordPress 4.9.6。

我在这里缺少什么?

谢谢。

2 个答案:

答案 0 :(得分:2)

问题是附件有post_status =='继承'不'发布'所以我会尝试添加

$query['post_status'] = [ 'publish', 'inherit' ];

到$ args。

请注意,这可能会获得更多结果,因为post_type =='post',post_status =='inherit'也将被返回。你应该做两个单独的查询。这只是对合并查询无法按预期工作的原因的解释。

原因

$query['post_type'] = 'attachment';

作品是WordPress在get_posts()

中有以下代码
if ( empty( $r['post_status'] ) )
    $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
}

答案 1 :(得分:0)

你能试试吗

$args = array(
  'post_type'   => array('post', 'attachment')
);

$multi_posts = get_posts( $args );

请务必检查自定义帖子类型的名称。