Sep 202014
 

I’m writing code for a wordpress plugin of mine, and needed for the get_posts() function to return ALL posts. Here’s what I was using before:

$args = array(
‘post_type’ => ‘post’,
‘post_status’ => ‘publish’,
);
$posts = get_posts($args);

According to the wordpress documentation, the ‘posts_per_page’ option for this function has a default value of 5, meaning you’ll get at most 5 results back. I looked to see if there was a way to show all posts, and I couldn’t find it documented anywhere.

However, I did see some people provide code that had that value set at -1. I thought it might be a way of specifying no limit, so I tried it, and it’s working. Here’s what I use now:

$args = array(
‘post_type’ => ‘post’,
‘post_status’ => ‘publish’,
‘posts_per_page’ => -1
);
$posts = get_posts($args);

It may not be a documented features, but it’s working for now. Give it a shot if you’re looking to have get_posts() return an unlimited number of posts.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)