0

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

1

I've configure permastructure for posts to /blog/%postname%/, I've created a page with slug about-us and I used the code bellow to register team_member CPT and it is working. I think you had a extra "/" in the rewrite argument. Also, you don't need to works with global $wp_rewrite; if you set the rewrite argument when the post type is registered, WordPress will create the rewrite rules for you. I've also made some comments to save non-needed code:

add_action("init", "register_cpt_team_member");
function register_cpt_team_member() {

    $labels = array(
        //If "label" is set, the "name" in "labels" argument is usually not needed
        //'name'                => _x( 'Team Members', 'Post Type General Name', 'client' ),
        // [...]
    );
    $args = array(
        //label should a plural descriptive name
        'label'               => __( 'Team Members', 'client' ),
        //description should include ...... a description ;)
        'description'         => __( 'Custom post type to manage team members', 'client' ),
        //As the only "labels" set was "name" and it overriden by general "label" you don't need this
        //'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'thumbnail' ),
        //hierarchical is by default false, you don't need this
        //'hierarchical'      => false,
        'public'              => true,
        //show_ui is by default set to same value of "public", so you don't need this
        //'show_ui'             => true,
        //show_in_menu by default set to same value of "show_ui", so you don't need this
        //'show_in_menu'             => true,
        //show_in_nav_menus is by default set to same value of "public", so you don't need this
        //'show_in_nav_menus'   => true,
        //show_in_admin_bar by default set to same value of "show_in_menu", so you don't need this
        //'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        //can_export is by default set to true, so you don't need this
        //'can_export'          => true,
        'has_archive'         => true,
        //exclude_from_search is by default set to opposite of "public", so you don't need this
        //'exclude_from_search' => false,
        //publicly_queryable is by default set to same value of "public" argumente, so you don't need this
        //'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'rewrite' => array (
            'slug' => '/about-us/team',
            'with_front' => false
        )
    );

    register_post_type( 'team_member', $args );

}

So, the above code can be written like this with the same result:

add_action("init", "register_cpt_team_member");
function register_cpt_team_member() {

    $args = array(
        'label'               => __( 'Team Members', 'client' ),
        'description'         => __( 'Custom post type to manage team members', 'client' ),
        'supports'            => array( 'title', 'editor', 'thumbnail' ),
        'public'              => true,
        'menu_position'       => 5,
        'has_archive'         => true,
        'capability_type'     => 'page',
        'rewrite' => array (
            'slug' => '/about-us/team',
            'with_front' => false
        )
    );

    register_post_type( 'team_member', $args );

}
  • Thanks for your answer - unfortunately, it did not change the behavior for me. I get a 404. – Lars Dec 18 '14 at 11:52
  • 1
    I've tested it again and it works perfectly. Remember to flush the rewrite rules after save the new code and before you try to load the custom post type. You can do that by visiting wp-admin/options-permalink.php in your WordPress installation and clicking "Save". – cybmeta Dec 18 '14 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.