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?
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:
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 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 am Using This For Custom Pagination and its work fine
// Define this for any Template like template-newsletter
This is the solution that worked for me, using part of the original code by nurain and the answer by Jan Fabry:
You Can Display you custom post type using this method your pagination can work !!!