I currently grab an array of user data from a query. I then use a foreach to loop through them and apply styles and divs to output them to the page. This all works great but i would like to paginate the array into the wordpress paged option. So if the user has set the max posts per page to be 10 and the array contains 20 results i want to stop at 10 and have a link to view the next page which would show 11-20. I have done something similar in the past with the standard wordpress loop with a if have posts thing with:

<? $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;?>

<?php if ( have_posts() ) : ?>
$limit = get_option('posts_per_page');

$args = array(
    'paged' => $paged,
'post_per_page' => $limit
<?php while (have_posts()) : the_post(); $counter++; ?>

Then i would have the prev / next links:

<?php if ($counter == $limit) { ?>
      <div class="greyBtn nextBtn"><?php next_posts_link('<span>Next &raquo;</span>') ?></div>
<?php } ?>
<?php if ($paged > 1) { ?>
    <div class="greyBtn previousBtn"><?php previous_posts_link('<span>&laquo; Prev</span>') ?></div>
<?php } ?>

I want to do this same logic for my foreach:

<? foreach ($all_members as $member) {?>
     //Do Loop Stuff
   <?php } ?>

Any ideas on how to do this?

  • Is there some code missing from the above, where is $paged set? – t31os Mar 8 '11 at 15:54
  • Yes sorry, just added it in to the top. – estern23 Mar 8 '11 at 15:57
  • This <?php if ( have_posts() ) : ?> should be after the query posts call, else you're checking the existing query, then altering it. Aside from that, what specifically isn't working for you? No results on page 2? Something else? – t31os Mar 8 '11 at 16:22
  • I was only using this as an example of the logic i need to create for pagination around my foreach vs this if have posts... I dont need to get the above code working, looking to get some direction on how to paginate the array of users. Sorry if that wasnt clear before – estern23 Mar 8 '11 at 16:25

I ended up using this method...


This script will allow you to take an array and paginate across multiple pages. Works good for what i need. Just working on how to number each item in the array across the pages in a descending order...

  • Please describe the method rather than just linking to it. – EAMann Mar 8 '11 at 18:11
  • Updated with some more details. – estern23 Mar 8 '11 at 18:33
  • site leads to a 404 – EHerman Apr 28 '14 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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