I am customizing a wordpress installation with a custom post type and have a problem with the permalink structure. I added a team_member
CPT, and the contents should be visible in a specific URL: /about-us/team/%team_member%/
. The other pages of the installation should be visible as /%pagename%/
(e.g. /about-us/
), while the blog posts should be visible under /blog/%postname%/
.
The page works when the permalink structure is set to default, resulting in links like example.com/?team_member=john_doe
. When I activate the permalink-format /blog/%postname
, the pages and blog posts do work, but the team_member CPT does not work at all. Without any rewrite structure, the team_members are found under /blog/team_member/john_doe/
, resulting in a 404.
Even with a custom rewrite structure, the same happens. Currently (in development), I flush the rewrite cache every time after registering the CPT via flush_rewrite_rules( true );
. The following registers my CPT, called via an after_setup_theme
hook:
/**
* Register Custom Post Type
*/
function register_cpt_team_member() {
$labels = array(
'name' => _x( 'Team Members', 'Post Type General Name', 'client' ),
// [...]
);
$args = array(
'label' => __( 'team_member', 'client' ),
'description' => __( 'Team Members', 'client' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'rewrite' => array (
'slug' => '/about-us/team/',
'with_front' => false
)
);
register_post_type( 'team_member', $args );
global $wp_rewrite;
$permalink_structure = '/about-us/team/%team_member%/';
$wp_rewrite->add_rewrite_tag("%team_member%", '([^/]+)', "team_member=");
$wp_rewrite->add_permastruct('team_member', $permalink_structure, false);
}
I do not understand, why neither with nor without a rewritten permalink structure