Get All Post Types in Wordpress in Query_Posts

I'm using query_posts to get a list of most popular posts. I'm using several custom post types, and instead of including all of them in the query, I would like a query that just gets all of them, also if I create more.

This is what I have:

query_posts(array(
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
        'post_type' => array('posttype1', 'postype2', 'posttype3')
    )
);

If I don't include the post_type, it only gets the standard post type, post. Does anyone have an idea?

You can use 'post_type' => 'any' for fetching from all post types. See this documentation.

Note: It is highly recommended to use WP_Query rather than query_posts.

1
'post_type' => 'any',

This gets all posts except revisions.

So your query would be:

query_posts(array(
    'post_type' => 'any',
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
        'post_type' => array('posttype1', 'postype2', 'posttype3')
    )
);
0

If you want to use get_posts(), the only work around is to use 'post_type' => get_post_types()

Example that returns ALL posts of ANY post type:

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

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest