Let's say I'm doing a search on my site, and get 50 posts as a result. The pagination is set to 10 posts per page, so I have 5 pages of results.

When I'm on the first page of results, if I inspect the global var $posts, it gives me data for the 10 posts of the current results page only.

How can I access the data of ALL the results, no matter which page I'm on?

  • Is this assuming that you keep the pagination set to 10 posts per page?
    – icc97
    Commented May 31, 2012 at 9:05
  • @icc97 Yes, that's the idea, how to keep pagination and at the same time beeing able to access the data of ALL the results, no matter which results page is currently displayed.
    – mike23
    Commented May 31, 2012 at 9:08
  • The only thing I can think of is to have a second query without the pagination. If I come up with anything better I'll add an answer
    – icc97
    Commented May 31, 2012 at 9:10
  • Thanks, yes that's exactly what I'm trying also right now, but it seems ugly, I hope there's a better way.
    – mike23
    Commented May 31, 2012 at 9:13
  • Could you add the code that you're using that returns the filtered $posts?
    – icc97
    Commented May 31, 2012 at 9:23

1 Answer 1


The whole loop mechanism is based around having a filtered $posts array. So looking in The Loop documentation on multiple loops it gives a PHP explanation for the standard loop code you use:

 <?php if (have_posts()) : ?>
           <?php while (have_posts()) : the_post(); ?>    
           <!-- do stuff ... -->
           <?php endwhile; ?>
 <?php endif; ?>

The have_posts() and the_post() are convenience wrappers around the global $wp_query object, which is where all of the action is. The $wp_query is called in the blog header and fed query arguments coming in through GET and PATH_INFO. The $wp_query takes the arguments and builds and executes a DB query that results in an array of posts. This array is stored in the object and also returned back to the blog header where it is stuffed into the global $posts array (for backward compatibility with old post loops).

The have_posts() simply calls into $wp_query->have_posts() which checks a loop counter to see if there are any posts left in the post array. And the_post() calls $wp_query->the_post() which advances the loop counter and sets up the global $post variable as well as all of the global post data. Once we have exhausted the loop, have_posts() will return false and we are done.

So the global $posts is connected to the have_posts function. For that to work properly it can only contain the paginated list of posts. Also note that the $posts array is only there for backwards compatibility so its best not to access it directly anyway.

So it looks like you'll have to either set up your own pagination or create a separate object with all posts.

I'd suggest the latter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.