Top vs Bottom
Most custom rewrite rules should be added at the top so WordPress checks them before its default permalink rules.
Generate WordPress rewrite rules for custom post types, taxonomies, redirects, and custom regex patterns with copy-ready PHP.
/projects/my-project/<?php
/**
* Custom Post Type Rewrite Rules
* Post Type: project
* Archive URL: /projects/
* Single URL: /project/{slug}/
*
* Add to functions.php
*/
function register_project_post_type() {
register_post_type( 'project', [
'labels' => [
'name' => ucfirst( 'projects' ),
'singular_name' => ucfirst( 'project' ),
],
'public' => true,
'has_archive' => 'projects',
'rewrite' => [
'slug' => 'project',
'with_front' => false,
'feeds' => false,
'pages' => true,
],
'supports' => [ 'title', 'editor', 'thumbnail', 'excerpt' ],
'show_in_rest'=> true,
] );
}
add_action( 'init', 'register_project_post_type' );
function project_activation() {
register_project_post_type();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'project_activation' );
function add_project_rewrite_rules() {
add_rewrite_rule(
'^projects/page/([0-9]+)/?$',
'index.php?post_type=project&paged=$matches[1]',
'top'
);
}
add_action( 'init', 'add_project_rewrite_rules' );Most custom rewrite rules should be added at the top so WordPress checks them before its default permalink rules.
If you add custom query variables in the rewrite target, remember to register them with the query_vars filter.
After saving rewrite code, flush rules by visiting Settings > Permalinks and clicking Save Changes once.