Why does the problem in pagination happens?
Say you have a category named wiki
.
Now, when you have a category URL like: https://example.com/wiki
(instead of https://example.com/category/wiki
) and say page or post URL like:
https://example.com/a-page
or
https://example.com/category-slug/a-post
WordPress has really no way of knowing if https://example.com/wiki/page/2
is a the second page of another page / post, or the second page of a category archive (because wiki
may very well be a page slug or page
be a post slug under category wiki
).
This happens specifically because WordPress confuses with /page/2
part as a different multi-part paginated page or post.
So, instead of loading the next pages of your wiki
category archive, WordPress will try to load a paginated post with the slug page
and you’ll get a 404
(page not found) error instead of getting the second page of the category archive.
Before I give you the solution to this issue, first make sure you’ve got the permalink correct.
To achieve the necessary Category URL Structure:
The URL structure you wanted needs some work. So unless you’ve done it already, follow the instructions below to achieve the said URL structure:
# URL structure for Categories
https://example.com/category-slug
For this:
- Go to:
WordPress Admin Panel Menu
→Settings
. - Put a single dot (
.
) in theCategory base
text field. - Click Save Changes button.
# URL structure for Posts
https://example.com/category-slug/post-slug
For this:
- Go to:
WordPress Admin Panel Menu
→Settings
. - Select
Custom Structure
and enter/%category%/%postname%/
in theCustom Structure
text field. - Click Save Changes button.
Solving the Category Pagination Problem:
Some SEO plugins have solution to this problem. However, since this is a core URL structure issue of your site, it’s better not to depend on SEO plugins for this. Say, what if you want to change the SEO plugin in future and the other plugin has better options but don’t provide solution for this?
So it’s better to use a simple plugin that fixes only this one problem. You may use the following CODE and save it in your plugin directory with file name category-pagination-fix.php
and then activate the plugin named Category Pagination Fix
from the site’s admin panel:
<?php
/*
Plugin Name: Category Pagination Fix
Plugin URI: https://wordpress.stackexchange.com/a/311858/110572
Description: Fix category pagination
Version: 1.0.0
Author: Fayaz Ahmed
Author URI: https://www.fayazmiraz.com/
*/
function wpse311858_fix_category_pagination( $query_string = array() )
{
if( isset( $query_string['category_name'] )
&& isset( $query_string['name'] ) && $query_string['name'] === 'page'
&& isset( $query_string['page'] ) ) {
$paged = trim( $query_string['page'], "https://wordpress.stackexchange.com/" );
if( is_numeric( $paged ) ) {
// we are not allowing 'page' as a page or post slug
unset( $query_string['name'] );
unset( $query_string['page'] ) ;
// for a category archive, proper pagination query string is 'paged'
$query_string['paged'] = ( int ) $paged;
}
}
return $query_string;
}
add_filter( 'request', 'wpse311858_fix_category_pagination' );
Alternate solution:
The above solution works perfectly with pagination when you have category URL like:
https://example.com/category-slug/
and post URL like:
https://example.com/category-slug/post-slug
However, when you have subcategory URL like:
https://example.com/category-slug/sub-category-slug
or, when you have post URL like:
https://example.com/post-slug
the above simple solution doesn’t work.
In these sort of cases, you may use a plugin like Remove Category URL.
These sort of plugins make some fundamental URL rewrite changes, that’s why some default WordPress behaviour may not work as expected after you use one of these plugins. So use the solution that works best for you, but make sure that you don’t try to do the same thing in multiple different plugins.
Caution: Using these sort of headless archive URL (e.g. no
category
or similar term for category page links, notag
or similar term in for tag page links etc.) creates internal conflicts with page & post URL(s). WordPress engine needs a lot of internal checking to determine which to load and which not to. That process is resource hungry. So if your site has thousands of pages, posts, categories, tags etc. it’s better to use these terms in the URL.