Custom Rewrite Rules For Paged Archive Pages In WordPress
Understanding Pagination Rules for Archive Pages
WordPress archives like category, tag, date, and author pages display posts in a paginated format by default. The main query splits the total set of relevant posts into pages, with the number of posts per page set by the Reading Settings. Navigation links are automatically added allowing users to go to the next/previous page.
This default paginated archive works well in most cases. However, sometimes you may want to customize the way archive pages are split into pages. For example, you could have:
- Category archives paginated by post ID rather than date
- Author pages displaying newest posts first
- Tag pages with posts sorted alphabetically
For such custom pagination scenarios, WordPress’s default archive rewrite rules need to be overridden with your own custom rules. This guides the main query to split and order posts as per your needs.
Creating Custom Rewrite Rules for Archive Pages
Understanding the Key Elements of Rewrite Rules
Before creating custom archive pagination rules, it helps to understand how rewrite rules work in WordPress:
- The
add_rewrite_rule()
function allows adding custom rules - The first parameter defines the regex pattern to match against the request
- The second parameter sends the request to the specified query variables
- Rules added are checked before default WordPress rules
- Use the
add_rewrite_tag
to add custom variables if needed
Step-by-Step Guide to Adding Custom Rules
With the basics clear, here is how you can add your own pagination rules for archive pages:
- Hook into
'init'
action to register the rule when WordPress initializes - Prepare the match regex based on the archive URL structure needed
- Redirect to your custom query variable using
add_rewrite_tag()
- Fetch posts using the custom variable in custom query
This will override default behavior and allow custom pagination. Let’s look at some examples next.
Example Code for Common Custom Pagination Scenarios
Category Archive Sorted by Post ID
add_action( 'init', 'wpse_323485_cat_sort_init' ); function wpse_323485_cat_sort_init(){ add_rewrite_tag( '%my_sort%', '([^&]+)' ); add_rewrite_rule( '^category/([^/]+)/my_sort/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&my_sort=$matches[2]', 'top' ); }
The my_sort
variable allows you to control order. Fetch posts using it in custom query.
Author Archive Showing Newest Posts First
add_action( 'init', 'wpse_323433_author_sort' ); function wpse_323433_author_sort(){ add_rewrite_tag( '%author_order%', '([^&]+)' ); add_rewrite_rule( '^author/([^/]+)/author_order/?([0-9]{1,})/?$', 'index.php?author_name=$matches[1]&author_order=$matches[2]', 'top' ); }
The author_order
parameter can order posts as needed on author page.
Troubleshooting Custom Archive Rewrite Rules
Common Issues and Errors
Some common issues faced when working with custom archive rules:
- 500 internal server error – syntax errors in rewrite rule regex.
- 404 page not found – rewrite pattern doesn’t match request URL.
- Infinite loops – rewrite rule interfering with other rules.
- Bad pagination – posts split incorrectly between pages.
Tools for Debugging Rewrite Rules
WordPress offers tools to help debug such rewrite related problems:
global $wp_rewrite
– inspect currently defined rulesadd_rewrite_rule('...', 'top')
– make rule get priorityflush_rewrite_rules()
– clear rules and rebuild- Enable
Save Queries
option to check SQL ran
Tips for Simplifying Complex Rulesets
Some tips to simplify rule debugging:
- Test with one rule at a time before combining
- Keep dynamic elements like variables at minimum
- Print current matching vars to check if rules work
- Reference default rules for pattern inspiration
- Use tools like Regex101.com to validate regex
Alternative Approaches to Custom Archive Pagination
While custom rewrite rules allow granular control, here are some alternative approaches you can try as well:
Plugins that Add Pagination Controls
Plugins like Archive Post Sorter and advancement Custom Archive allow configuring default archive ordering and pagination from the admin dashboard itself. This avoids having to write your own regex rules.
Modifying the Main Query with Filters
Filters like pre_get_posts
allow modifying default archive queries on the fly. This provides a simplified approach in some cases compared to rewrite rules.
Ajax-Powered Infinite Scroll Functionality
Infinite scroll based on Ajax can be implemented to load additional archive posts seamlessly as the user scrolls down. This provides an improved reader experience over traditional pagination.