Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does get_posts() return only 5 matching posts, when it should return 9?

Tags:

wordpress

global $post; 
$cat1=get_cat_ID('test1'); 
$cat2=get_cat_ID('test2'); 
$myrecentposts = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat1,-$cat2",'showposts' => 5));
$myrecentposts2 = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat1,-$cat2"));
$myrecentpostscount = count($myrecentposts2);
echo $myrecentpostscount;

The value of the echo is 5 (the correct value should be 9). The only way I can get it to return the correct value for the post count is to change the $myrecentposts2 calculation as follows...

$myrecentposts2 = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat1,-$cat2",'showposts' => 999));
like image 732
Scott B Avatar asked Jan 25 '10 18:01

Scott B


2 Answers

The Wordpress codex says get_posts has a default posts_per_page value of 5.

To remove this limit use posts_per_page = -1.

To remove this limit you can use nopaging = true.

like image 124
roman Avatar answered Sep 23 '22 14:09

roman


Look at the get_posts() documentation on Codex, you can see there's a parameter for number of posts you want to display:

The parameter is: 'posts_per_page'
Usage: 'posts_per_page'=> -1 // for removing the limit. This will fetch all the posts.

update: 'nopaging' => true ist the way to go with newer versions

like image 39
Kunal Mazumder Avatar answered Sep 19 '22 14:09

Kunal Mazumder