42

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.

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?

  • After answering I reread your question...Did you mean your pagenavi plugin wasn't showing up or that the pagination was bringing you to a 404? – eileencodes Nov 30 '10 at 20:02
  • WP Page Navi isn't displaying on the posts with similar queries (filtered by more than one meta key). It's appearing on all other pages like archives, blog, etc. – nurain Nov 30 '10 at 20:45
39

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.

  • I don't know when PageNavi made this change in their plugin, but ever since Wordpress 3.0 I've had to hijack the $wp_query variable to get pagination to show on custom loops. Glad I could help! :D – PNMG Nov 30 '10 at 21:36
  • 8
    wp_pagenavi() now also accepts the query argument, so instead of overwriting $wp_query you can write wp_pagename( array( 'query' => $my_query ) ). – Jan Fabry Feb 17 '11 at 14:15
  • This solution worked for me. I had another loop before this one, so I had to add a wp_reset_query() before the code in the example. – Ryan Aug 23 '11 at 20:25
  • See this answer for another possible solution for Numerical Pagination with custom queries using WP_Query();. – Michael Ecklund Apr 18 '17 at 18:37
11

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

  • I don't think I have anything named the same. I actually created a new page called "Quick Move In - 60 Days" and added the loop in by creating a page template for it. – nurain Nov 30 '10 at 21:08
7

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'       => __('&#9668;'),
        'next_text'       => __('&#9658;'),
        '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>
  • Hi custom_pagination() function define in theme functions file or plugin functions file. and after you put this code in your template file Define this after comment part in above code =Define this for any Template like template-newsletter for any Template like template-newsletter if any query so let me know here. – Adnan Limdiwala Feb 21 '17 at 6:55
  • Any issue regarding this Ans I hope its work fine for you – Adnan Limdiwala Apr 7 '17 at 9:23
3

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; ?>
2

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; ?>

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.