This is different from the question: How to display categories of my Custom Post Type?
I have a custom post type - podcasts. I have added the taxonomy "category" as I want my 'podcasts' to share the 'category' taxonomy with my posts. I have an archive page (archive-podcasts.php) that displays the podcasts at the url /podcasts/, but it shows all the categories. I would like to display my podcasts in a single category at a time, such as at mysite.com/podcasts/category/mycategory/
or even better: mysite.com/category/mycategory/podcasts/
Ideally it would use my existing archive-podcasts.php template.
Here's my custom post type creation:
function podcast_post_type() {
// Set UI labels for Podcast post Type
$labels = array(
//redacted my labels because they aren't important here.
);
// Set other options for Podcast post Type
$args = array(
'label' => __( 'podcasts', 'understrap' ),
'description' => __( 'Podcasts', 'understrap' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'category' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'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' => 'post',
);
// Registering your Podcast post Type
register_post_type( 'podcasts', $args );
}
The following code seemed promising but didn't work:
add_action('init', 'category_cpt_rewrites');
function category_cpt_rewrites() {
$custom_post_types = array('podcasts'); //some example post types
foreach ( $custom_post_types as $post_type ) {
$rule = '^' . $post_type . '/category/(.+?)/?$';
$rewrite = 'index.php?post_type=' . $post_type . '&category_name=$matches[1]';
add_rewrite_rule($rule,$rewrite,'top');
}
}
I actually kind of expected this would work automatically - that by adding the 'category' taxonomy to my post type,I thought Wordpress would have a url where I could access my podcast categories.
I've lost a lot of time to this, and it seems like it should be simple - any help is appreciated!