【问题标题】:Get ALL post types in WordPress in query_posts在 query_posts 中获取 WordPress 中的所有帖子类型
【发布时间】:2015-08-13 19:18:51
【问题描述】:

我正在使用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。有人有想法吗?

【问题讨论】:

    标签: wordpress custom-post-type


    【解决方案1】:

    您可以使用'post_type' => 'any' 从所有帖子类型中获取。请参阅本文档。 http://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters

    注意:强烈建议使用WP_Query 而不是query_postshttps://wordpress.stackexchange.com/a/1755/27998

    【讨论】:

    • 谢谢,工作就像一个魅力 - 我会调查 WP_query,感谢您指出我的方向。
    【解决方案2】:
    'post_type' => 'any',
    

    这将获取除修订​​之外的所有帖子。 https://developer.wordpress.org/reference/classes/wp_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')
        )
    );
    

    【讨论】:

      【解决方案3】:

      如果你想使用get_posts(),唯一的解决方法是使用'post_type' => get_post_types()

      返回任何帖子类型的所有帖子的示例:

      $posts = get_posts([
        'post_type' => get_post_types(),
        'post_status' => 'publish',
        'numberposts' => -1,
      ]);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多