The problem
I was just messing around with a custom post type loop, and noticed that pagination wasn’t showing up at all. Neither posts_nav_link() nor the WP-PageNavi plugin was working and showing up navigation links.
I was basically using a custom WP_Query like this:
$custom_query = new WP_Query( $args ); while ($custom_query->have_posts()) : ...
The solution
global $wp_query; $clone_page_total = $wp_query->max_num_pages; $wp_query->max_num_pages = $custom_query->max_num_pages; wp_pagenavi(); $wp_query->max_num_pages = $clone_page_total;
Just make sure to put your pagination function call (wp_pagenavi() in this case) before we restore the $wp_query property to it’s original value (in line 7).
What does it do?
The problem is that WordPress pagination functions need the $wp_query object. They don’t care for a custom $custom_query object. We basically just pretend there is a $wp_query object by cloning the needed property max_num_pages to $wp_query.
Requirements
- WordPress 3.0 (if you use custom post types)
Tags: custom, Custom Post Type, Pagination, Post, WP_Query, WP-PageNavi