Rewrite rules in WordPress: custom post type slugs, endpoints and flushing
WordPress turns pretty URLs into queries with a table of rewrite rules: regular expressions that map a path like /books/my-book/ to query variables like post_type=book&name=my-book. The table is generated by the WP_Rewrite class and stored in the rewrite_rules option. Custom post types, taxonomies and endpoints add rules to it, and the classic bug is that the new rules are not there until the table is rebuilt.
Problem: A new custom post type with the slug books returns 404 for /books/my-book/, and a URL segment like /books/my-book/reviews/ is not recognised at all.
Solution: Flush the rewrite rules once after registering new rules: in the plugin’s activation hook, or by saving Settings → Permalinks. Add URL segments with add_rewrite_endpoint() and give the post type the matching endpoint mask.
The post type and its slug
<?php
add_action( 'init', 'ha_register_book_post_type' );
function ha_register_book_post_type() {
register_post_type( 'book', array(
'label' => 'Books',
'public' => true,
'has_archive' => true,
'rewrite' => array(
'slug' => 'books', // /books/my-book/
'with_front' => false, // ignore a /blog/ prefix of the post permalinks
'ep_mask' => EP_PERMALINK, // let endpoints work on books (see below)
),
) );
}
Flushing once, not on every request
<?php
register_activation_hook( __FILE__, function () {
ha_register_book_post_type(); // the rules must exist before the flush
flush_rewrite_rules();
} );
register_deactivation_hook( __FILE__, function () {
unregister_post_type( 'book' );
flush_rewrite_rules(); // remove the plugin's rules
} );
flush_rewrite_rules() rebuilds the whole table, saves it in the database and, on Apache, rewrites .htaccess. Calling it on init does that on every page view: slow, and with several requests at once it can leave the rules half written. For a theme, flush on after_switch_theme; during development use wp rewrite flush or save the Permalinks page.
Endpoints: an extra URL segment
An endpoint adds /name/ or /name/value/ to existing URLs, for example a reviews tab of a book:
<?php
add_action( 'init', function () {
add_rewrite_endpoint( 'reviews', EP_PERMALINK | EP_PAGES );
} );
add_action( 'template_redirect', function () {
if ( is_singular( 'book' ) && false !== get_query_var( 'reviews', false ) ) {
// /books/my-book/reviews/ -> load a reviews template
include get_stylesheet_directory() . '/book-reviews.php';
exit;
}
} );
The endpoint is registered as a query variable automatically. Without a value (/reviews/) it is an empty string, which is why the check compares with false. EP_PERMALINK covers normal posts; a custom post type gets the endpoint only with 'ep_mask' => EP_PERMALINK in its rewrite settings, as above. Like any new rule, an endpoint needs a flush.
Looking at the rules
wp rewrite list --match=/books/my-book/reviews/
shows which rule matches a URL and which query it produces. Two other permalink cases are in adding the term name to a custom post type slug and a custom post type and a page with the same slug.
NOTE: The order of rules matters: the first match wins. A post type slug that is also the slug of a page, or a very general rule added at the top, can take over URLs of other content. When a URL suddenly shows the wrong content, check the matching rule first.