Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: get all posts of a custom type

Tags:

wordpress

I have this strange issue. I want to fetch all posts that are of a custom type, here's my snippet.

$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(); 

This only gets me 6 of them, while I have more than 50 records matching that criteria in the database. Can anyone tell me where I have gone wrong?

Many thanks!

like image 734
Michael Avatar asked Aug 11 '14 06:08

Michael


People also ask

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

'posts_per_page' => -1, Add this to the WP_QUERY array of arguments and it should return all of the posts of this custom post type.

How do I get custom post type data in WordPress?

First, you can simply go to Appearance » Menus and add a custom link to your menu. This custom link is the link to your custom post type. Don't forget to replace 'example.com' with your own domain name and 'movies' with your custom post type name.

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 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.


2 Answers

'posts_per_page' => -1,

Add this to the WP_QUERY array of arguments and it should return all of the posts of this custom post type.

like image 138
jono Avatar answered Sep 22 '22 18:09

jono


This get all posts of a custom type using get_posts:

$posts = get_posts([   'post_type' => 'custom',   'post_status' => 'publish',   'numberposts' => -1   // 'order'    => 'ASC' ]); 
like image 45
Andrea Avatar answered Sep 18 '22 18:09

Andrea