The rewrite rules are wrong. For example, you want to rewrite from community/tag/easter
to 'index.php?pagename=community&stencil-tag=easter'
, so the regex should contain community/tag/
and not only tag/
. Also, \d
match only digits but the tag value is a string. Same apply to the rewrite for stencil-tag. You could use .
to match any character, both numeric and strings.
add_action('init','add_community_rewrite_rules');
function add_community_rewrite_rules() {
add_rewrite_rule(
'^community/tag/(.*)$',
'index.php?pagename=community&stencil-tag=$matches[1]',
'top'
);
add_rewrite_rule(
'^community/type/(.*)$',
'index.php?pagename=community&stencil-type=$matches[1]',
'top'
);
}
Although those rewrite rules can be correct, I think you should use another approach. Use the default archive page generated by WordPress for the projects
post type. If you need a specific template for it, create the file archive-projects.php
in your theme folder. You can rewrite the slug from projects
to community
when registering the post type, if you want that URL for the post type archive:
add_action( 'init', function() {
$args = array(
//Your args here
'rewrite' => array( 'slug' => 'community' ),
);
register_post_type( 'projects', $args );
} );
Then, when registering the taxonomies, you can build the rewrite rules also:
add_action( 'init', function() {
//Custom post type
$args = array(
//Your args here
'rewrite' => array( 'slug' => 'community' ),
);
register_post_type( 'projects', $args );
//Custom taxonomies: stencil-tag
$args = array(
//Your args here
'rewrite' => array( 'slug' => 'community/tag' ),
);
register_taxonomy( 'stencil-tag', 'projects', $args );
//Custom taxonomies: stencil-tag
$args = array(
//Your args here
'rewrite' => array( 'slug' => 'community/type' ),
);
register_taxonomy( 'stencil-type', 'projects', $args );
} );
Now the rewrite rules are handled by WordPres automatically and you don’t need to render a page and make a secondary query for the posts inside the page as they are fetched in the main query.