Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 842
pshoeg Avatar asked May 31 '15 06:05

pshoeg


People also ask

How do I get all post data in WordPress?

As so: $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'order' => $sort_by, 'orderby' => 'title', 'post_status' => 'publish', 'tag' => $tags, 'ignore_sticky_posts' => 1, ); This will make Query get all posts in your table.

How do I get all posts from a custom post type?

$query = new WP_Query(array( 'post_type' => 'custom', 'post_status' => 'publish' )); while ($query->have_posts()) { $query->the_post(); $post_id = get_the_ID(); echo $post_id; echo "<br>"; } wp_reset_query();

How do I find category posts in WordPress?

Now, if you want to display all your posts from a specific category on a separate page, WordPress already takes care of this for you. To find the category page, you simply need to go to Posts » Categories » View page and click on the 'View' link below a category.

How do I display custom post type categories in WordPress?

To display your custom post types on the same category page as your default posts, you need to add this code into your theme's functions. php or a site-specific plugin. $post_type = array ( 'nav_menu_item' , 'post' , 'movies' ); // don't forget nav_menu_item to allow menus to work!


1 Answers

You can use 'post_type' => 'any' for fetching from all post types. See this documentation. http://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters

Note: It is highly recommended to use WP_Query rather than query_posts. https://wordpress.stackexchange.com/a/1755/27998

like image 131
Nilambar Sharma Avatar answered Sep 27 '22 19:09

Nilambar Sharma