The following assumes that the Podcast Archive will have a different slug than the Podcast Page. Otherwise, WordPress won’t know which resource to serve when the user visits /podcasts/
.
Give the Taxonomy a default term. This ensures that every Podcast lands in a category and will have a category in the permalink. See register_taxonomy()
for full args.
register_taxonomy( 'podcast_category', array('podcast'), array(
'rewrite' => array( 'slug' => 'podcasts-category' ),
'default_term' => array(
'name' => esc_html__( 'Uncategorized' ),
),
) );
Add an arbitrary placeholder in the Post Type rewrite slug.
register_post_type( 'podcast' , array(
'rewrite' => array(
'slug' => 'podcasts/%podcast_category%',
'with_front' => false
),
'has_archive' => 'podcasts-all',
) );
Replace the arbitrary placeholder in the Postcast URL.
/**
* Post Type Link
* Replace placeholder with post term.
*
* @param String $post_link
* @param WP_Post $post
*
* @return String $post_link
*/
function podcast_post_link( $post_link, $post ) {
if( 'podcast' !== $post_type ) {
return $post_link;
}
$terms = wp_get_object_terms( $post->ID, 'podcast_category' );
if( ! ( empty( $terms ) || is_wp_error( $terms ) ) ) {
$post_link = str_replace( '%podcast_category%' , $terms[0]->slug , $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'podcast_post_link', 10, 2 );
Save permalinks. Head to Settings > Permalinks and click Save. This will refresh the permalink structure. What you should end up with:
Podcast Page: /podcasts/
Podcast Archive: /podcasts-all/
Podcast Category: /podcast-category/uncategorized/
Podcast Singular: /podcasts/uncategorized/podcast-slug/
Note that in your register_post_type()
you’re calling has_archive
twice. The 2nd has_archive => true
is overwriting your post type archive slug.