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'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:

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

share|improve this question
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

3 Answers 3

up vote 19 down vote accepted

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;
   'cat' => 6,
   'posts_per_page' => 5,
$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();   


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.

share|improve this answer
Wow, nevermind. I figured it out. Works great! Thanks! –  nurain Nov 30 '10 at 21:18
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
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

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

share|improve this answer
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

This is the solution that worked for me, using part of the original code by nurain and the answer by Jan Fabry:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$myquery = new WP_Query(
        'posts_per_page' => '2',
        // add any other parameters to your wp_query array

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>                        

<!-- 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; ?>
share|improve this answer

Your Answer


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.