Fixing Common WP-PageNavi Issues: Pagination Not Working with Custom Queries
Content
WP-PageNavi is a powerful plugin for improving WordPress pagination, but a frequent issue arises when users implement custom queries. Many developers report that pagination links either don't appear, show the wrong page count, or fail to navigate beyond the first page. This guide explains why this happens and provides the most effective solutions based on community troubleshooting.
Why Custom Queries Break Pagination
The core issue is that WP-PageNavi primarily interacts with the global `$wp_query` object. When you create a custom `WP_Query` or use `query_posts()`, you're working with a separate query object. If not properly configured, the plugin cannot accurately calculate the number of pages or generate correct pagination links. Common symptoms include:
- Navigation showing "Page 1 of 1" when more pages exist
- Clicking page 2, 3, etc., displays the same posts as page 1 or a 404 error
- Pagination links not appearing at all, even with "Always Show" enabled
- The current page indicator being stuck on page 1
Solution 1: Properly Pass the Custom Query Object
The most reliable fix is to explicitly pass your custom query object to the `wp_pagenavi()` function. This tells the plugin which query to use for calculating pagination.
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$custom_query = new WP_Query( array(
'post_type' => 'your_post_type',
'posts_per_page' => 5,
'paged' => $paged
) );
while ( $custom_query->have_posts() ) : $custom_query->the_post();
// Your loop content
endwhile;
wp_pagenavi( array( 'query' => $custom_query ) );
wp_reset_postdata();
Key Points:
- Always use `get_query_var( 'paged' )` for the `paged` parameter in your query arguments.
- Pass the custom query variable to `wp_pagenavi()` using the `query` parameter in an array.
- Use `wp_reset_postdata()` after the loop to restore the global `$post` object.
Solution 2: Correctly Set the Paged Parameter for Front Pages
On static front pages, WordPress uses `page` instead of `paged` for the query variable. If your custom query is on the homepage, use this conditional logic:
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
Solution 3: Avoid query_posts() and Use WP_Query Instead
The `query_posts()` function is not recommended for custom queries as it modifies the main query and often causes pagination conflicts. The WordPress codex strongly advises against its use. Instead, always create a new instance of `WP_Query` for secondary loops.
Solution 4: Handle Special Cases with pre_get_posts
If you're modifying queries using `pre_get_posts` (e.g., excluding sticky posts on paginated pages), be aware that this can affect the post count. Ensure your logic correctly accounts for the number of posts being displayed on each page to prevent 404 errors on later pages.
When These Solutions Might Not Apply
If you've implemented these solutions and pagination still fails, consider these possibilities:
- Permalink Issues: Ensure your permalink structure is set to something other than "Plain" (Settings > Permalinks).
- Theme Conflicts: Switch to a default theme (like Twenty Twenty-One) temporarily to rule out theme-specific issues.
- Plugin Conflicts: Deactivate other plugins to see if one is interfering with query execution.
- Custom SQL Queries: If you're using `$wpdb->get_results()`, you must manually set `$wp_query->max_num_pages` and `$wp_query->found_posts` for the pagination to calculate correctly.
By correctly implementing custom queries and explicitly telling WP-PageNavi which query to use, you can resolve most pagination issues. The key is ensuring your custom query contains the correct `paged` parameter and that the plugin is pointed to the right query object.
Related Support Threads Support
-
[Plugin: WP-PageNavi] Current page are wrong using $wpdb->get_results(https://wordpress.org/support/topic/plugin-wp-pagenavi-current-page-are-wrong-using-wpdb-get_results/
-
Working with multiple loop indexhttps://wordpress.org/support/topic/working-with-multiple-loop-index/
-
Page 2 sends me to Index.phphttps://wordpress.org/support/topic/page-2-sends-me-to-indexphp/
-
Extra blank pages when query excludes categorieshttps://wordpress.org/support/topic/extra-blank-pages-when-query-excludes-categories/
-
[Plugin: WP-PageNavi] Trying to fix "Warning: You forgot to set the 'paged' query varhttps://wordpress.org/support/topic/plugin-wp-pagenavi-trying-to-fix-warning-you-forgot-to-set-the-paged-query-var/
-
Use with WP_Query (not query_posts)https://wordpress.org/support/topic/use-with-wp_query-not-query_posts/
-
Filtered have posts creates problemhttps://wordpress.org/support/topic/filtered-have-posts-creates-problem/
-
WP-PageNavi is showing the first page onlyhttps://wordpress.org/support/topic/wp-pagenavi-is-showing-the-first-page-only/
-
[Plugin: WP-PageNavi] Pages and custom queryhttps://wordpress.org/support/topic/plugin-wp-pagenavi-pages-and-custom-query/
-
URL not changing stuck on first pagehttps://wordpress.org/support/topic/url-not-changing-stuck-on-first-page/
-
When I go to page 2, I see the same posts as on page 1!https://wordpress.org/support/topic/when-i-go-to-page-2-i-see-the-same-posts-as-on-page-1/
-
How to use Multiple Custom Queries with WP-PageNavi?https://wordpress.org/support/topic/how-to-use-multiple-custom-queries-with-wp-pagenavi/
-
Using WP-PageNavi on home page with filtered categories…https://wordpress.org/support/topic/using-wp-pagenavi-on-home-page-with-filtered-categories/
-
wp-admin/admin-ajax.php?page=2https://wordpress.org/support/topic/wp-adminadmin-ajaxphppage2/
-
show pagenavi for custom pagehttps://wordpress.org/support/topic/show-pagenavi-for-custom-page/
-
two navigation in one page !https://wordpress.org/support/topic/two-navigation-in-one-page/
-
Pagenavi: Page2 or next page goes to home pagehttps://wordpress.org/support/topic/pagenavi-page2-or-next-page-goes-to-home-page/
-
not working with custom SWP_Queryhttps://wordpress.org/support/topic/not-working-with-custom-swp_query/
-
[Plugin: WP-PageNavi] Custom query and form submithttps://wordpress.org/support/topic/plugin-wp-pagenavi-custom-query-and-form-submit/
-
Solution to wp-pagenavi not working, content the same on all pageshttps://wordpress.org/support/topic/solution-to-wp-pagenavi-not-working-content-the-same-on-all-pages/
-
Only Showing One Page of Excerptshttps://wordpress.org/support/topic/only-showing-one-page-of-excerpts/
-
WP-PAGENAVI – CUSTOM-POST-TYPE – returns me to home (index)https://wordpress.org/support/topic/wp-pagenavi-custom-post-type-returns-me-to-home-index/
-
Stays on Page 1https://wordpress.org/support/topic/stays-on-page-1/
-
[Plugin: WP-PageNavi] Only Showing First Page of Excerptshttps://wordpress.org/support/topic/plugin-wp-pagenavi-only-showing-first-page-of-excerpts/
-
Pagination works but "current" page is always 1https://wordpress.org/support/topic/pagination-works-but-current-page-is-always-1/
-
[Plugin: WP-PageNavi] Wp page navi on custom post type pagehttps://wordpress.org/support/topic/plugin-wp-pagenavi-wp-page-navi-on-custom-post-type-page/
-
[Plugin: WP-PageNavi] Possible bug when using WP PageNavi with custom querieshttps://wordpress.org/support/topic/plugin-wp-pagenavi-possible-bug-when-using-wp-pagenavi-with-custom-queries/
-
wp-navi works great everywhere except the front(index.php) page!https://wordpress.org/support/topic/wp-navi-works-great-everywhere-except-the-frontindexphp-page/
-
wp-pagenavi won't go past first page.https://wordpress.org/support/topic/wp-pagenavi-wont-go-past-first-page/
-
Navigation buttons always return me to the first pagehttps://wordpress.org/support/topic/navigation-buttons-always-return-me-to-the-first-page/
-
[Plugin: WP-PageNavi] No posts showing.https://wordpress.org/support/topic/plugin-wp-pagenavi-no-posts-showing/
-
PageNavi inside single post typehttps://wordpress.org/support/topic/pagenavi-inside-single-post-type/
-
"Always Show Page Navigation" Not Workinghttps://wordpress.org/support/topic/always-show-page-navigation-not-working/
-
wp-pagenavi in custom queryhttps://wordpress.org/support/topic/wp-pagenavi-in-custom-query/
-
Permalink conflict with PageNavihttps://wordpress.org/support/topic/permalink-conflict-with-pagenavi/
-
PageNavi showing wrong number of pageshttps://wordpress.org/support/topic/pagenavi-showing-wrong-number-of-pages/
-
wp-pagenavi stuck on page 1 when permalinks are postnamehttps://wordpress.org/support/topic/wp-pagenavi-stuck-on-page-1/
-
Redirectshttps://wordpress.org/support/topic/redirects-10/
-
Navigation on home page is linking to search querieshttps://wordpress.org/support/topic/navigation-on-home-page-is-linking-to-search-queries/
-
[Plugin: WP-PageNavi] Not changing posts — Correct query.https://wordpress.org/support/topic/plugin-wp-pagenavi-not-changing-posts-correct-query/
-
Fix – Page Navigation current page stuck on page (1)https://wordpress.org/support/topic/fix-page-navigation-current-page-stuck-on-page-1/
-
WP_Query with Page Navihttps://wordpress.org/support/topic/wp_query-with-page-navi/
-
Page not foundhttps://wordpress.org/support/topic/page-not-found-145/
-
Wrong number of pages with 'post__not_in'https://wordpress.org/support/topic/wrong-number-of-pages-with-post_not_in/
-
Not moving from page 1https://wordpress.org/support/topic/not-moving-from-page-1/
-
Navigation links show same posts no matter which pagehttps://wordpress.org/support/topic/navigation-links-show-same-posts-no-matter-which-page/
-
[Plugin: WP-PageNavi] Pagination not working when I use a custom queryhttps://wordpress.org/support/topic/plugin-wp-pagenavi-pagination-not-working-when-i-use-a-custom-query/
-
WP-PageNavi with custom query and $paged variablehttps://wordpress.org/support/topic/wp-pagenavi-with-custom-query-and-paged-variable/
-
[Plugin: WP-PageNavi] Request page number as integerhttps://wordpress.org/support/topic/plugin-wp-pagenavi-request-page-number-as-integer/
-
if statement to check on what page your are to link different csshttps://wordpress.org/support/topic/if-statement-to-check-on-what-page-your-are-to-link-different-css/
-
[Plugin: WP-PageNavi] Unable to display the correct contenthttps://wordpress.org/support/topic/plugin-wp-pagenavi-unable-to-display-the-correct-content/
-
[Plugin: WP-PageNavi] wp_pagenavi on *.php custom pages in roothttps://wordpress.org/support/topic/plugin-wp-pagenavi-wp_pagenavi-on-php-custom-pages-in-root/