我用的是 query_posts
来获取最受欢迎的帖子列表。我使用了几种自定义的帖子类型,我不希望把所有的帖子都包含在查询中,我希望查询能得到所有的帖子,如果我创建了更多的帖子,我也希望能得到所有的帖子。
这就是我的情况。
query_posts(array(
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post_type' => array('posttype1', 'postype2', 'posttype3')
)
);
如果我不包含 post_type
,它只得到标准的职位类型。post
. 有谁有办法吗?
你可以使用 'post_type' => 'any'
用于从所有职位类型中获取。请看这个文档。http:/codex.wordpress.orgClass_ReferenceWP_Query#Type_Parameters。
注:强烈建议使用 WP_Query
而非 query_posts
. https:/wordpress.stackexchange.coma175527998。
query_posts(array(
'post_type' => get_post_types(),
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post_type' => array('posttype1', 'postype2', 'posttype3')
)
);
get_post_types() 可以解决这个问题
'post_type' => 'any',
这将获得除修订版以外的所有帖子。https:/developer.wordpress.orgreferenceclasseswp_query#post-type-parameters。
所以你的疑问会是:
query_posts(array(
'post_type' => 'any',
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post_type' => array('posttype1', 'postype2', 'posttype3')
)
);