Fixing WP-PageNavi 404 Errors and Pagination Not Working
Content
If you've landed here, you're likely frustrated because your WP-PageNavi pagination links are leading to a 404 "Page Not Found" error, or clicking on page 2 just redirects you back to the homepage without showing new posts. This is one of the most common issues users face with this plugin, and it's almost always related to how the WordPress query is handled in your theme files, not a bug in the plugin itself.
Why This Happens
The core of the problem lies in a misunderstanding of how WordPress handles pagination. WordPress uses a main query to determine what content to display based on the URL. The WP-PageNavi plugin simply generates links based on that query. When you create a custom query using query_posts or new WP_Query without properly handling the 'paged' parameter or resetting the main query, you break the inherent connection between the URL and the content, leading to 404 errors or incorrect page displays.
Common Solutions
1. Correctly Use the 'paged' Parameter
A frequent mistake is using 'page' instead of 'paged' for the query argument. For front pages, homepages, and archive pages, you must use 'paged'.
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 5,
'paged' => $paged, // NOT 'page'
);
$custom_query = new WP_Query( $args );
2. Avoid query_posts()
The WordPress Codex strongly advises against using query_posts() as it modifies the main query and can cause significant pagination issues. It should be replaced with a secondary WP_Query or by using the pre_get_posts hook to modify the main query safely.
3. Pass the Correct Query to wp_pagenavi()
If you are using a custom query object (like $custom_query), you must pass that specific query to the wp_pagenavi() function. If you don't, the function will try to paginate the main WordPress query, which won't match your custom loop.
// Your custom loop using $custom_query
have_posts() ) : $custom_query->the_post(); ?>
... // Your post content
// Correctly paginate the custom query
$custom_query ) ); ?>
4. Check Your Permalink Structure
Sometimes, a simple permalink refresh can resolve odd 404 issues. Go to Settings > Permalinks in your WordPress admin and simply click "Save Changes" without making any changes. This flushes the rewrite rules and can often clear up conflicts.
5. For Category & Archive Templates
On category, tag, or other archive pages, it's often best to use the main query. If you need to modify it, use the pre_get_posts hook in your theme's functions.php file instead of creating a new query in the template. This preserves all the original pagination and taxonomy data.
Example of a Correct Implementation
Here is a safe way to implement a custom loop with WP-PageNavi on a Page Template.
'post',
'category__in' => array( 6 ),
'posts_per_page' => 5,
'paged' => $paged
);
// Create a new custom query
$custom_query = new WP_Query( $args );
// Run the custom loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) : $custom_query->the_post();
// Display your post content here
the_title();
the_excerpt();
endwhile;
// Paginate the CUSTOM query
wp_pagenavi( array( 'query' => $custom_query ) );
// Reset postdata to restore the main query
wp_reset_postdata();
else :
echo 'No posts found.';
endif;
?>
By following these steps, you should be able to resolve the 404 and pagination issues. The problem is almost always in the theme's template code, not the WP-PageNavi plugin. If you continue to have problems, carefully check your query arguments and ensure you are passing the correct query object to the wp_pagenavi() function.
Related Support Threads Support
-
Same topic: 404 after first page…https://wordpress.org/support/topic/same-topic-404-after-first-page-1/
-
Problem With A Custom Themehttps://wordpress.org/support/topic/problem-with-a-custom-theme/
-
Query category__in error page nexthttps://wordpress.org/support/topic/query-category_in-error-page-next/
-
Pagination not working in archive page (archive.php) and custom page templatehttps://wordpress.org/support/topic/pagination-not-working-in-archive-page-archivephp-and-custom-page-template/
-
PageNavi not loading beyond page 1https://wordpress.org/support/topic/pagenavi-not-loading-beyond-page-1/
-
Navigation not working with post_status=futurehttps://wordpress.org/support/topic/navigation-not-working-with-post_statusfuture/
-
[Plugin: WP-PageNavi] 404 error in the tagshttps://wordpress.org/support/topic/plugin-wp-pagenavi-404-error-in-the-tags/
-
[Plugin: WP-PageNavi] wp-pagenavi throws error 404https://wordpress.org/support/topic/plugin-wp-pagenavi-wp-pagenavi-throws-error-404/
-
[Plugin: WP-PageNavi] wp navi in my schedulehttps://wordpress.org/support/topic/plugin-wp-pagenavi-wp-navi-in-my-schedule/
-
Navigation wont work pages.https://wordpress.org/support/topic/navigation-wont-work-pages/
-
error when used wp-pagenavihttps://wordpress.org/support/topic/error-when-used-wp-pagenavi/
-
Posts are shown but title is "Page is not found"https://wordpress.org/support/topic/posts-are-shown-but-title-is-page-is-not-found/
-
Yet another Page Not Found question (category page)!https://wordpress.org/support/topic/yet-another-page-not-found-question-category-page/
-
News not change with the page.https://wordpress.org/support/topic/news-not-change-with-the-page/
-
Paging not workinghttps://wordpress.org/support/topic/paging-not-working-2-2/
-
Not working with 2 or more categorieshttps://wordpress.org/support/topic/not-working-with-2-or-more-categories/
-
[Plugin: WP-PageNavi] Wp navi doesn't work in my categoryhttps://wordpress.org/support/topic/plugin-wp-pagenavi-wp-navi-doesnt-work-in-my-category/
-
Now Working on static homepagehttps://wordpress.org/support/topic/now-working-on-static-homepage/
-
Page 2 returns “page not found” errorhttps://wordpress.org/support/topic/page-2-returns-page-not-found-error/
-
[Plugin: WP-PageNavi] Page navi doesnt work – 404 Errorhttps://wordpress.org/support/topic/plugin-wp-pagenavi-page-navi-doesnt-work-404-error-1/
-
my code is thishttps://wordpress.org/support/topic/my-code-is-this/
-
[Plugin: WP-PageNavi] also displaying only the first pagehttps://wordpress.org/support/topic/plugin-wp-pagenavi-also-displaying-only-the-first-page/
-
[Plugin: WP-PageNavi] WP-pagenavi noy working with custom permalinkshttps://wordpress.org/support/topic/plugin-wp-pagenavi-wp-pagenavi-noy-working-with-custom-permalinks/
-
[Plugin: WP-PageNavi] Not working on home pagehttps://wordpress.org/support/topic/plugin-wp-pagenavi-not-working-on-home-page/
-
Once Again: Page not found on second and further pageshttps://wordpress.org/support/topic/one-again-page-not-found-on-second-and-further-pages/
-
when i click page '2', it goes back to home pagehttps://wordpress.org/support/topic/when-i-click-page-2-it-goes-back-to-home-page/
-
[Sahifa Theme WP-PageNavi] page 2 link to page 1https://wordpress.org/support/topic/sahifa-theme-wp-pagenavi-page-2-link-to-page-1/
-
wp_pagenavi on archive.phphttps://wordpress.org/support/topic/wp_pagenavi-on-archivephp/
-
[Plugin: WP-PageNavi] wp-navi throws error 404 on category-{cat-name}.phphttps://wordpress.org/support/topic/plugin-wp-pagenavi-wp-navi-throws-error-404-on-category-cat-namephp/
-
Page 2 sends me to home page, pages 3 and above work OKhttps://wordpress.org/support/topic/page-2-sends-me-to-home-page-pages-3-and-above-work-ok/
-
cannot get pagenavi to workhttps://wordpress.org/support/topic/cannot-get-pagenavi-to-work/
-
[Plugin: WP-PageNavi] conflict with 'post_status' => 'future'.https://wordpress.org/support/topic/plugin-wp-pagenavi-conflict-with-post_status-future/
-
Pagenavi not working in author.php wordpresshttps://wordpress.org/support/topic/pagenavi-not-working-in-authorphp-wordpress/
-
Same topic: 404 after first page…https://wordpress.org/support/topic/same-topic-404-after-first-page/
-
Can't display second pagehttps://wordpress.org/support/topic/cant-display-second-page/
-
[Plugin: WP-PageNavi] 404 error for next or numbered pagehttps://wordpress.org/support/topic/plugin-wp-pagenavi-404-error-for-next-or-numbered-page/
-
Error 404 with sub-category pagehttps://wordpress.org/support/topic/error-404-with-sub-category-page/
-
[Plugin: WP-PageNavi] PageNavi Shows 404 Errorhttps://wordpress.org/support/topic/plugin-wp-pagenavi-pagenavi-shows-404-error/
-
[Plugin: WP-PageNavi] Work on the home page, on the other does not workhttps://wordpress.org/support/topic/plugin-wp-pagenavi-work-on-the-home-page-on-the-other-does-not-work/
-
[Plugin: WP-PageNavi] the plugin does not workhttps://wordpress.org/support/topic/plugin-wp-pagenavi-the-plugin-does-not-work/
-
wp-pagenavi calls index.php instead of category.php with paged greater than 1https://wordpress.org/support/topic/wp-pagenavi-calls-index-php-instead-of-category-php-with-paged-greater-than-1/
-
Page 2 of category page in only some categories not workinghttps://wordpress.org/support/topic/page-2-of-category-page-in-only-some-categories-not-working/
-
Works in category but not on indexhttps://wordpress.org/support/topic/works-in-category-but-not-on-index/
-
[Plugin: WP-PageNavi] wp_pagenavi not workinghttps://wordpress.org/support/topic/plugin-wp-pagenavi-wp_pagenavi-not-working/
-
work on Query pagehttps://wordpress.org/support/topic/work-on-query-page/
-
Wp-Navi does not work on Homepagehttps://wordpress.org/support/topic/wp-navi-does-not-work-on-homepage/
-
[Plugin: WP-PageNavi] PageNavi doesn't work at home page !!!https://wordpress.org/support/topic/plugin-wp-pagenavi-pagenavi-doesnt-work-at-home-page/
-
[Plugin: WP-PageNavi] PageNavi does not work on the home pagehttps://wordpress.org/support/topic/plugin-wp-pagenavi-pagenavi-does-not-work-on-the-home-page/
-
Error 404 when clicking "2"https://wordpress.org/support/topic/error-404-when-clicking-2/