1

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!

  • Did you flush your Permalinks? – filipecsweb Mar 1 '17 at 2:51
  • Yeah, I tried that. I also tried switching between different different permalink structures, but no change. – Jordan Davis Mar 1 '17 at 3:05
2

Try this and let us know if it works:


function custom_category_rewrite() {

    /**
     * Here you build an array containing the post type keys that you want
     * the rule to work for.
     */
    $custom_post_types = array('podcast');

    foreach ( $custom_post_types as $post_type ) {
        /**
         * Adds rewrite rule which will rewrite sth like http://yoursite.com/podcast/category/category-name/ to
         * http://yoursite.com/index.php?post_type=podcast&category_name=category-name
         */
        add_rewrite_rule( "^$post_type/category/(.+)/?$", 'index.php?post_type=' . $post_type . '&category_name=$matches[1]', 'top' );
    }

    flush_rewrite_rules();

}

add_action( 'init', 'custom_category_rewrite' );

Put the code within your functions.php.

Pay attention to the comments along the snippet.

Try accessing something like http://yoursite.com/podcast/category/category-name/ to perform a test.

  • Didn't get this till after I'd fixed it, but this is essentially what I did. Thanks! – Jordan Davis Mar 1 '17 at 17:33

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.