- This topic is empty.
- AuthorPosts
-
August 11, 2014 at 6:58 am #9965
michael
ParticipantI 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!
August 11, 2014 at 7:10 am #9968bobadevv
ParticipantThe number of posts to return are set under settings > reading
You can pass the number of posts for your query to return using.
'posts_per_page' => 'number of posts'
August 11, 2014 at 7:11 am #9970jono
Participant'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.
July 9, 2017 at 6:40 am #9966joseacat
ParticipantIt is advisable to use an integer instead of ‘-1’ For example:
'posts_per_page' => 999999,
July 17, 2017 at 4:12 am #9969andrea
ParticipantThis get all posts of a custom type using
get_posts
:$posts = get_posts([ 'post_type' => 'custom', 'post_status' => 'publish', 'numberposts' => -1 // 'order' => 'ASC' ]);
August 29, 2018 at 12:41 pm #9967mustra
ParticipantYou should never use:
'posts_per_page' => -1
It slow and not effective, if you are talking about SQL Query speeds. So it is much better to use some large integer.
This is a performance hazard. What if we have 100,000 posts? This could crash the site. If you are writing a widget, for example, and just want to grab all of a custom post type, determine a reasonable upper limit for your situation.
More details here:
https://10up.github.io/Engineering-Best-Practices/php/#performance - AuthorPosts
- You must be logged in to reply to this topic.