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?
Answer:-
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.
0 comments:
Post a Comment