Custom URL structures with add_rewrite_rule() and query variables
Post types, taxonomies and endpoints produce their URLs automatically. When you need a structure that none of them gives, for example /docs/installation/requirements/ where the first part is a section and the second a document, you write the rule yourself: a regular expression for the path and the query it stands for.
Problem: Documentation pages should have URLs like /docs/{section}/{document}/, and /docs/{section}/ should list the documents of that section.
Solution: Add the rules with add_rewrite_rule() on init, register the custom query variable with the query_vars filter (or add_rewrite_tag()), use it in pre_get_posts, and flush the rules once on activation.
The rules
The example uses a post type doc and a taxonomy doc_section, both registered on init with 'rewrite' => false, because the URLs come from our rules:
<?php
add_action( 'init', 'ha_docs_rewrite_rules' );
function ha_docs_rewrite_rules() {
// /docs/section/document/
add_rewrite_rule(
'^docs/([^/]+)/([^/]+)/?$',
'index.php?post_type=doc&ha_section=$matches[1]&name=$matches[2]',
'top'
);
// /docs/section/
add_rewrite_rule(
'^docs/([^/]+)/?$',
'index.php?post_type=doc&ha_section=$matches[1]',
'top'
);
}
The first argument is matched against the path without the domain and without the leading slash. $matches[1] is the first group in parentheses. 'top' puts the rule before the core rules, so a page with the slug docs does not catch these URLs first.
The query variable
WordPress ignores query variables it does not know. Register yours:
<?php
add_filter( 'query_vars', function ( $vars ) {
$vars[] = 'ha_section';
return $vars;
} );
add_rewrite_tag( '%ha_section%', '([^/]+)' ) does the same and also lets you use %ha_section% in permalink structures.
Using it in the query
<?php
add_action( 'pre_get_posts', function ( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
$section = $query->get( 'ha_section' );
if ( $section ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => 'doc_section',
'field' => 'slug',
'terms' => sanitize_title( $section ),
),
) );
}
} );
For a single document the query already has the name; the section only narrows it. To make links to documents match the new structure, filter post_type_link and build the URL from the section term and the post slug.
Flushing
New rules work only after the rewrite table is rebuilt, once:
<?php
register_activation_hook( __FILE__, function () {
ha_docs_rewrite_rules();
flush_rewrite_rules();
} );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
Why never to flush on every request and how endpoints work is explained in rewrite rules, custom post type slugs and endpoints.
NOTE: Test the regular expression with wp rewrite list --match=/docs/installation/requirements/. A pattern that is too general (^([^/]+)/([^/]+)/?$) at the top would catch the URLs of normal posts, pages and categories, so always start your patterns with a fixed prefix like docs/.