例如,我通过register_post_type()
函数在我的主题中注册3个帖子类型(或更多):
现在我想获取所有不属于Product
帖子类型的帖子(帖子,页面,日程,下载的所有帖子)。
我不想使用'post_type' => array('post','page','agenda','download')
因为我将来会注册更多帖子类型。
对于帖子类型,'category__not_in'
是否没有等效参数?
答案 0 :(得分:2)
我不确定尝试什么是个好主意,但你可以这样做:
首先,你get all the existing post_types:
$post_types = get_post_types(['public' => true]);
然后,您从该数组中删除您不想查询的帖子类型:
$excluded_posttypes = ['product'];
$post_types = array_diff($post_types, $excluded_posttypes);
然后将其用作WP_Query调用的参数:
'post_type' => $post_types,
答案 1 :(得分:0)
也许你需要这个:https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
有一个排除类别的例子,也许这可以用于帖子类型。
如果上面的钩子无法使用,您可以在构建查询之前尝试以下操作:
这样,每个新注册的帖子类型都将包含在查询中。请注意默认的WP帖子类型'发布'也将在get_post_types
函数中返回。