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.

I have a wordpress page that has a series of listings and a searchbox at the top. The search allows the user to filter posts by creating a custom WP_Query object based on what they selected. I am wondering if there is a way I can have this be paginated so that there aren't hundreds of results displaying on the page.

Basically I have an if statement to check if the form is submitted which then populates the selected fields into an array and passes it to the meta_query

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if(isset($_POST['submit'])){
    $the_query = new WP_Query(array(
          'post_type' => 'business',
          'post_status' => 'publish', 
          'taxonomy' => 'business-type',
          'business-type' => 'attraction',
          'posts_per_page' => 15,
          'orderby' => 'title',
          'order' => 'ASC',
          'meta_query' => $arrays,
          'paged' => $paged
        ));
    }else{
    $the_query = new WP_Query(array(
        'post_type' => 'business',
        'post_status' => 'publish', 
        'taxonomy' => 'business-type',
        'business-type' => 'attraction',
        'posts_per_page' => 15,
        'orderby' => 'title',
        'order' => 'ASC',
        'paged' => $paged
       ));

The I loop through the query using while ( $the_query->have_posts() ) : $the_query->the_post();

and after endwhile; I have my pagination

if($the_query->max_num_pages>1){?>
    <div class="pagination">
    <?php
      if ($paged > 1) { ?>
        <a href="<?php echo '?paged=' . ($paged -1); //prev link ?>"><</a>
      <?php }
      for($i=1;$i<=$the_query->max_num_pages;$i++){
        if($paged==$i){?>
          <span class="current"><?php echo $i; ?></span>
        <?php }else{?>
          <a href="<?php echo '?paged=' . $i; ?>" class="inactive"><?php echo $i;?></a>
        <?php
      }
    }
    if($paged < $the_query->max_num_pages){?>
        <a href="<?php echo '?paged=' . ($paged + 1); //next link ?>">></a>
    <?php } ?>
    </div>
share|improve this question
    
Almost certainly "yes", but post your code please. –  s_ha_dum Jun 13 '13 at 18:07
    
Edit: Added my code –  tyler Jun 13 '13 at 18:26

2 Answers 2

I was going to create my own question and answer it, but this looks as good a place as any. The way I solved this, is by using $_SESSION. Keep in mind I am using infinite-scroll, so I can tell when the page is being fetched via ajax as opposed to being actually navigated to in the browser.

So here's the steps/pseudo-code:

  1. Clear Session Variables
  2. Run "Default" loop and get html
  3. User filters results -> get results from new loop
  4. Set Session Variables so page knows which loops to run
  5. User scrolls down -> get results from current page, but for next page
  6. This page displays a loop conditionally based on the session variables
  7. ??????
  8. Profit

I would just post my code, but it is pretty large so I'd rather not. If you can't figure it out, let me know though and I will post the skeleton of the code.

share|improve this answer

Based on the code you posted, you are not passing any meta parameters through to the pagination function. You will need to create links like :

<a href="<?php get_permalink(). '?paged=' . ($paged + 1) . '&meta_key='.$something.'&meta_value='.$somethingelse; //next link ?>">Link Text</a>

I assume your are processing that meta information for your initial query somewhere in order to populate $arrays but unless you pass that information along to the subsequent queries you will end up with the default search on pages 2 and on and not a paginated list restricted to the selected item.

Note that I added get_permalink to your link. You were using a relative URL and those will cause a lot of heartache in WordPress.

You might also look into paginate_links instead of rolling your own pagination. It is handy.... not necessary, but handy.

share|improve this answer
    
I will definitely add get_permalink(), thanks for catching that. I never thought about passing my meta key/values to the link itself. The main problem with that is my search form has checkboxes so I am actually passing the meta_query an array that contains all fields of the form some(checkboxes) being arrays themselves. –  tyler Jun 13 '13 at 20:06
    
But you are not passing that form data when someone clicks a link. The initial form submission works, I assume, but you need to pass that data along with the pagination links-- you have to if you want the pagination to work, unless you use something like $_SESSION or cookies which are more complicated solutions. You can pass arrays through a GET string, just use square brackets-- &meta_value[]=value1&meta_value[]=value2 and so on. –  s_ha_dum Jun 13 '13 at 20:12
    
And you are not just shoving that form data into the query unprocessed are you? That is a dangerous thing to do if you are. –  s_ha_dum Jun 13 '13 at 20:13
    
I will give this a go and see what happens. Currently I pass an array of fields like so: $arrays=array( array( 'key' => 'attraction_type', 'value' => $attraction_type, 'compare' => 'LIKE' ), ... more arrays where the variable corresponds to the field value. Then I query the posts and filter out the ones that don't have the selected checkbox amenities. Previously I passed the checkbox array to my meta_query array and it weighed the loadtime down and crashed the site. –  tyler Jun 13 '13 at 21:06

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.