Wordpress - Pagination not working with custom loop
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 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
//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>