Back to Community

Fixing Common WP-PageNavi Issues: Pagination Not Working with Custom Queries

52 threads Sep 16, 2025 PluginWp-pagenavi

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