WordPress Permalink Structure: “/custom-post-type/custom-taxonomy/post-slug”

First, register your taxonomy and set the slug argument of rewrite to shows:

register_taxonomy(
    'show_category',
    'show',
    array(
        'rewrite' => array( 'slug' => 'shows', 'with_front' => false ),
        // your other args...
    )
);Code language: PHP (php)

Next, register your post type and set the slug to shows/%show_category%, and set has_archive argument to shows:

register_post_type(
    'show',
    array(
        'rewrite' => array( 'slug' => 'shows/%show_category%', 'with_front' => false ),
        'has_archive' => 'shows',
        // your other args...
    )
);Code language: PHP (php)

Last, add a filter to post_type_link to substitute the show category in individual show permalinks:

function wpa_show_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'show' ){
        $terms = wp_get_object_terms( $post->ID, 'show_category' );
        if( $terms ){
            return str_replace( '%show_category%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );Code language: PHP (php)

EDIT: Forgot the has_archive argument of register_post_type above, that should be set to shows.

Reference: https://wordpress.stackexchange.com/a/108647


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.