Pagination not working with custom loop

I’ve got a custom loop that I’m using to display some Real Estate listings that will be available within 60 days. I’m calling it with the following function:

<?php 
$sixtydays = date('Y/m/d', strtotime('+60 days'));
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new PostsOrderedByMetaQuery(array(
  'post_type' => array('post', 'real-estate'),
  'meta_key' => 'Time Available',
  'meta_compare' => '<=',
  'meta_value' => $sixtydays,
  'paged' => $paged,
  'orderby_meta_key' => 'Price',
  'orderby_order'    => 'ASC'
));
?>
<?php while ($query->have_posts()) : $query->the_post(); ?>

While the loop works great, I can’t get it to paginate. It shows the first 10 (my default) posts but doesn’t show the pagination. The only way to display all posts is to show them on one page by adding 'posts_per_page' => -1, I have similar loops on other pages that have no problem paginating. The only difference with this one is that there are two meta keys that are filtering the posts.

Read More

I’m using WP Page Navi for this and the rest of my pages. I’m closing the loop and adding the pagination using the following code:

<?php endwhile; // End the loop. Whew. ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query(); ?>

How can I go about fixing this?

Related posts

Leave a Reply

5 comments

  1. I’ve run into this problem with PageNavi before. My solution is to hijack the $wp_query variable temporarily and then reassign it after closing the loop. An exmaple:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args=array(
       'post_type'=>'post',
       'cat' => 6,
       'posts_per_page' => 5,
       'paged'=>$paged
    );
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query($args);
    
    /* PageNavi at Top */
    if (function_exists('wp_pagenavi')){wp_pagenavi();}
    if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();   
    
    /* DO STUFF IN THE LOOP */
    
    endwhile; endif;
    /* PageNavi at Bottom */
    if (function_exists('wp_pagenavi')){wp_pagenavi();}
    $wp_query = null;
    $wp_query = $temp;
    wp_reset_query(); ?>
    

    The last step is to reassign the $wp_query variable to what is was originally and then reset the query back to start.

    *Edit:*Fixed php tag. Good eye sniper.

  2. I had a similar issue earlier today…

    Do you have a custom post type and a page or post with the same slug? Meaning is the url of a page you have /real-estate and the custom post type url rewrite at /real-estate ?

    If that’s the case you can’t have 2 with the same url or else wordpress gets confused.

    You either can change the url or try this http://wordpress.org/support/topic/pagination-with-custom-post-type-listing?replies=23#post-1637753. I chose to change my url, but someone on there wrote a custom query to get around the problem

  3. I am Using This For Custom Pagination and its work fine

    //paginations for newsletter
    
         //define in function file
          function custom_pagination($numpages = '', $pagerange = '', $paged='')  {
    
          if (empty($pagerange)) {
            $pagerange = 2;
          }
    
          /**
           * This first part of our function is a fallback
           * for custom pagination inside a regular loop that
           * uses the global $paged and global $wp_query variables.
           * 
           * It's good because we can now override default pagination
           * in our theme, and use this function in default queries
           * and custom queries.
           */
    
          if ($paged == '') {
              global $paged;
              if (empty($paged)) {
                $paged = 1;
              }
          }
          if ($numpages == '') {
            global $wp_query;
            $numpages = $wp_query->max_num_pages;
            if(!$numpages) {
                $numpages = 1;
            }
          }
    
          /** 
           * We construct the pagination arguments to enter into our paginate_links
           * function. 
           */
    
          $pagination_args = array(
            'base'            => get_pagenum_link(1) . '%_%',
            'format'          => 'page/%#%',
            'total'           => $numpages,
            'current'         => $paged,
            'show_all'        => false,
            'end_size'        => 1,
            'mid_size'        => $pagerange,
            'prev_next'       => true,
            'prev_text'       => __('◄'),
            'next_text'       => __('►'),
            'type'            => 'plain',
            'add_args'        => true,
            'add_fragment'    => '',
            'after_page_number' => '',
            'before_page_number' =>'',
            );
         $paginate_links = paginate_links($pagination_args);
    
          if ( $paginate_links ) {
            echo "<nav class='custom-pagination'>";
              //echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
              echo $paginate_links;
            echo "</nav>";
          }
        }
        ?>
    

    // Define this for any Template like template-newsletter

        <?php  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
                                   $newslatter_detail = array(
                                   'post_type' => 'newsletter',
                                   'post_status' => 'publish',
                                   'posts_per_page' =>4,
                                   'order' => 'ASC',
                                   //'orderby' =>'date',
                                   'paged' => $paged
                                   );
                                   $posts = new WP_Query( $newslatter_detail );
                                   $posts_array = get_posts( $newslatter_detail );
                                  if ( $posts -> have_posts() ) {
                                   while ( $posts->have_posts() ) : $posts->the_post();
                                   the_title();
                                   endwhile;
                            wp_reset_postdata();
                  } else { ?>
                     No Forum List found.
                    <?php } ?>
    
                  <div class="pagination">
                        <?php
                           if (function_exists(custom_pagination)) {
                            custom_pagination($posts->max_num_pages,"",$paged);
                           }
                           ?>
                     </div>
    
  4. This is the solution that worked for me, using part of the original code by nurain and the answer by Jan Fabry:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $myquery = new WP_Query(
        array(
            'posts_per_page' => '2',
            'paged'=>$paged
            // add any other parameters to your wp_query array
        )   
    );  
    ?>
    
    <?php
    if ($myquery->have_posts()) :  while ($myquery->have_posts()) : $myquery->the_post();
    ?>
    
    <!-- Start your post. Below an example: -->
    
    <div class="article-box">                               
    <h2 class="article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p class="article-excerpt"><?php echo(get_the_excerpt()); ?></p>                        
    </div>
    
    <!-- End of your post -->
    
    <?php endwhile; ?>
    <?php wp_pagenavi( array( 'query' => $myquery ) ); ?><!-- IMPORTANT: make sure to include an array with your previously declared query values in here -->
    <?php wp_reset_query(); ?>
    <?php else : ?>
    <p>No posts found</p>
    <?php endif; ?>
    
  5. You Can Display you custom post type using this method your pagination can work !!!

    <?php
      query_posts( array( 'post_type' => 'post', 'posts_per_page' => '2', 'paged' =>     get_query_var( 'paged' ) ) ); 
    
      if (have_posts() ) :  while (have_posts() ) : the_post(); 
    ?>
    <!-- Start your post. Below an example: -->
    <div class="article-box">                               
    <h2 class="article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p class="article-excerpt"><?php echo(get_the_excerpt()); ?></p>                        
    </div>
    <!-- End of your post -->
    <?php endwhile; ?>
    <?php wp_pagenavi(); ?><!-- IMPORTANT: make sure to include an array with your previously declared query values in here -->
    <?php wp_reset_query(); ?>
    <?php else : ?>
    <p>No posts found</p>
    <?php endif; ?>