Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Is this assuming that you keep the pagination set to 10 posts per page? –  icc97 May 31 '12 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 May 31 '12 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 May 31 '12 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 May 31 '12 at 9:13
    
Could you add the code that you're using that returns the filtered $posts? –  icc97 May 31 '12 at 9:23

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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