0

I have a custom post type with two taxonomies set. On my Episode CPT I have a rewrite arg set to "rewrite" => array( 'slug' => 'bctv', 'with_front' => false ). Then on my Taxonomy I also have rewrite args set to "rewrite" => array( 'slug' => 'bctv/%series%', 'with_front' => false ), "rewrite" => array( 'slug' => 'bctv/%series_type%', 'with_front' => false ). When said and done if I go to a series type tax term it would look like "www.site.com/bctv/series-type-term-name or www.site.com/bctv/series-term-name". I also have additional rewrite logic in place to handle adding CPT post name to the url, which would look like "www.site.com/bctv/series-type-term-name/cpt-post-name or www.site.com/bctv/series-term-name/cpt-post-name".

I have been able to get the Series Term to work with rewrites with out a problem. However, when applying those similar rewrites to my Series Type Term it is redirecting me back to the home page and not taxonomy-series_type.php template. I'm also have issues were the slug is getting duplicated in the permalink with clicking on the View link form admin. Screenshot attached:enter image description here

I should also mention that if I remove the rewrite arg from my series_type tax, then I can get to were I need to me "www.site.com/series_typ/tax-name". So I am 100% sure were I am falling short on my rewrite.

Here is my full source code:

Taxonomy

function bctv_register_episode_post_type_taxes() {

    /**
     * Taxonomy: Series Typess.
     */

    $labels = array(
        "name" => __( "Series Types", "bctv-post-type-ui" ),
        "singular_name" => __( "Series Types", "bctv-post-type-ui" ),
        "menu_name" => __( "Series Types", "bctv-post-type-ui" ),
        "all_items" => __( "Series Types", "bctv-post-type-ui" ),
        "edit_item" => __( "Edit Types", "bctv-post-type-ui" ),
        "view_item" => __( "View Types", "bctv-post-type-ui" ),
        "update_item" => __( "Update Series Type", "bctv-post-type-ui" ),
        "add_new_item" => __( "Add New Type", "bctv-post-type-ui" ),
        "new_item_name" => __( "New Series Type", "bctv-post-type-ui" ),
        "parent_item" => __( "Parent Type", "bctv-post-type-ui" ),
        "parent_item_colon" => __( "Parent Type:", "bctv-post-type-ui" ),
        "search_items" => __( "Search Types", "bctv-post-type-ui" ),
        "popular_items" => __( "Popular Series Type", "bctv-post-type-ui" ),
        "add_or_remove_items" => __( "Add or Remove Series Type", "bctv-post-type-ui" ),
        "choose_from_most_used" => __( "Choose From most used Types", "bctv-post-type-ui" ),
        "not_found" => __( "Type not found", "bctv-post-type-ui" ),
        "no_terms" => __( "No Types", "bctv-post-type-ui" ),
        "items_list_navigation" => __( "Series Type", "bctv-post-type-ui" ),
        "items_list" => __( "Series Type List", "bctv-post-type-ui" ),
    );

    $args = array(
        "label" => __( "Series Type", "bctv-post-type-ui" ),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => true,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => array( 'slug' => 'bctv/%series_type%', 'with_front' => false ),
        "show_admin_column" => true,
        "show_in_rest" => true,
        "rest_base" => "series_type",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => false,
    );

    register_taxonomy( "series_type", array( "episodes" ), $args );
    flush_rewrite_rules();

    /**
     * Taxonomy: Series.
     */

    $labels = array(
        "name" => __( "Series", "bctv-post-type-ui" ),
        "singular_name" => __( "Series", "bctv-post-type-ui" ),
        "menu_name" => __( "Series", "bctv-post-type-ui" ),
        "all_items" => __( "Series", "bctv-post-type-ui" ),
        "edit_item" => __( "Edit Series", "bctv-post-type-ui" ),
        "view_item" => __( "View Series", "bctv-post-type-ui" ),
        "update_item" => __( "Update Series", "bctv-post-type-ui" ),
        "add_new_item" => __( "Add New Series", "bctv-post-type-ui" ),
        "new_item_name" => __( "New Series", "bctv-post-type-ui" ),
        "parent_item" => __( "Parent Series", "bctv-post-type-ui" ),
        "parent_item_colon" => __( "Parent Series:", "bctv-post-type-ui" ),
        "search_items" => __( "Search Series", "bctv-post-type-ui" ),
        "popular_items" => __( "Popular Series", "bctv-post-type-ui" ),
        "separate_items_with_commas" => __( "Popular Series:", "bctv-post-type-ui" ),
        "add_or_remove_items" => __( "Add or Remove Series", "bctv-post-type-ui" ),
        "choose_from_most_used" => __( "Choose From most used Series", "bctv-post-type-ui" ),
        "not_found" => __( "Series not found", "bctv-post-type-ui" ),
        "no_terms" => __( "No Series", "bctv-post-type-ui" ),
        "items_list_navigation" => __( "Series", "bctv-post-type-ui" ),
        "items_list" => __( "Series Type List", "bctv-post-type-ui" ),
    );

    $args = array(
        "label" => __( "Series", "bctv-post-type-ui" ),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => true,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => array( 'slug' => 'bctv/%series%', 'with_front' => false ),
        "show_admin_column" => true,
        "show_in_rest" => true,
        "rest_base" => "series",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => false,
        );

    register_taxonomy( "series", array( "episodes" ), $args );
    flush_rewrite_rules();

    /**
     * Taxonomy: Actors.
     */

    $labels = array(
        "name" => __( "Actors", "bctv-post-type-ui" ),
        "singular_name" => __( "Actor", "bctv-post-type-ui" ),
        "menu_name" => __( "Actors", "bctv-post-type-ui" ),
        "all_items" => __( "All Actors", "bctv-post-type-ui" ),
        "edit_item" => __( "Edit Actor", "bctv-post-type-ui" ),
        "view_item" => __( "View Actor", "bctv-post-type-ui" ),
        "update_item" => __( "Update Actor", "bctv-post-type-ui" ),
        "add_new_item" => __( "Add New Actor", "bctv-post-type-ui" ),
        "new_item_name" => __( "New Actor", "bctv-post-type-ui" ),
        "search_items" => __( "Search Actors", "bctv-post-type-ui" ),
        "popular_items" => __( "Popular Actors", "bctv-post-type-ui" ),
        "add_or_remove_items" => __( "Add or Remove Actors", "bctv-post-type-ui" ),
        "choose_from_most_used" => __( "Choose from the most used Actors", "bctv-post-type-ui" ),
        "not_found" => __( "Actor Not Found", "bctv-post-type-ui" ),
        "no_terms" => __( "No Actors", "bctv-post-type-ui" ),
        "items_list" => __( "Actors List", "bctv-post-type-ui" ),
    );

    $args = array(
        "label" => __( "Actors", "bctv-post-type-ui" ),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => true,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => array( 'slug' => 'actors', 'with_front' => true ),
        "show_admin_column" => true,
        "show_in_rest" => true,
        "rest_base" => "actor",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => false,
    );

    register_taxonomy( "actor", array( "episodes" ), $args );
    flush_rewrite_rules();
}

add_action( "init", "bctv_register_episode_post_type_taxes" );

Post Type

function bctv_register_episode_post_type() {

    /**
     * Post Type: Episodes.
     */

    $labels = array(
        "name" => __( "Episodes", "bctv-post-type-ui" ),
        "singular_name" => __( "Episode", "bctv-post-type-ui" ),
        "menu_name" => __( "Episodes", "bctv-post-type-ui" ),
        "all_items" => __( "All Episodes", "bctv-post-type-ui" ),
        "add_new" => __( "Add New", "bctv-post-type-ui" ),
        "add_new_item" => __( "Add New Episode", "bctv-post-type-ui" ),
        "edit_item" => __( "Edit Episode", "bctv-post-type-ui" ),
        "new_item" => __( "New Episode", "bctv-post-type-ui" ),
        "view_item" => __( "View Episode", "bctv-post-type-ui" ),
        "view_items" => __( "View Episodes", "bctv-post-type-ui" ),
        "search_items" => __( "Search Episodes", "bctv-post-type-ui" ),
        "not_found" => __( "No Episode Found", "bctv-post-type-ui" ),
        "not_found_in_trash" => __( "No Episodes Found in Trash", "bctv-post-type-ui" ),
        "parent_item_colon" => __( "Parent Episode:", "bctv-post-type-ui" ),
        "featured_image" => __( "Featured Episode Image", "bctv-post-type-ui" ),
        "set_featured_image" => __( "Set Featured Image", "bctv-post-type-ui" ),
        "remove_featured_image" => __( "Remove Featured Image", "bctv-post-type-ui" ),
        "use_featured_image" => __( "Use Featured Image", "bctv-post-type-ui" ),
        "archives" => __( "Episode Archive", "bctv-post-type-ui" ),
        "insert_into_item" => __( "Insert into Episode", "bctv-post-type-ui" ),
        "uploaded_to_this_item" => __( "Upload to this Episode", "bctv-post-type-ui" ),
        "filter_items_list" => __( "Filter Episode List", "bctv-post-type-ui" ),
        "items_list_navigation" => __( "Episode List Navigation", "bctv-post-type-ui" ),
        "items_list" => __( "Episode List", "bctv-post-type-ui" ),
        "attributes" => __( "Episode Attributes", "bctv-post-type-ui" ),
        "name_admin_bar" => __( "Episodes", "bctv-post-type-ui" ),
        "parent_item_colon" => __( "Parent Episode:", "bctv-post-type-ui" ),
    );

    $args = array(
        "label" => __( "Episodes", "bctv-post-type-ui" ),
        "labels" => $labels,
        "description" => "This will be a list of Episodes (oldest to newest) for the BCTV Original Series the user is viewing.",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "delete_with_user" => false,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "exclude_from_search" => false,
        "capability_type" => "page",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => array( 'slug' => 'bctv', 'with_front' => false ),
        "query_var" => true,
        "menu_position" => 9,
        "menu_icon" => "dashicons dashicons-video-alt2",
        "supports" => array( "title", "editor", "thumbnail", "page-attributes" ),
        "taxonomies" => array( "series", "series_type", "actor", "post_tag" ),
    );

        register_post_type( "episodes", $args );
        flush_rewrite_rules();
    }

    add_action( 'init', 'bctv_register_episode_post_type' );

URL Rewrite Filters

//ToDO: can't get this working. 404's to home page
function episodes_series_type_cpt_generating_rule($wp_rewrite) {
    $series_type_rules = array();

    $series_type_terms = get_terms( array(
        'taxonomy' => 'series_type',
        'hide_empty' => false,
    ) );

    $post_type = 'episodes';

    foreach ($series_type_terms as $series_type_term) {
        $series_type_rules['bctv/' . $series_type_term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&' . $post_type. '=$matches[1]&name=$matches[1]';
    }

    // merge with global rules
    $wp_rewrite->rules = $series_type_rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'episodes_series_type_cpt_generating_rule');

function series_type_change_link( $permalink, $post ) {

    if( $post->post_type == 'episodes' ) {
        $episode_type_terms = get_the_terms( $post, 'series_type' );
        $term_type_slug = '';
        if( ! empty( $episode_type_terms ) ) {
            foreach ( $episode_type_terms as $term_type ) {
                $term_type_slug = $term_type->slug;
            }
        }

        $permalink = get_home_url() ."/bctv/" . $term_type_slug . '/' . $post->post_name;
    }
    return $permalink;
}
add_filter('post_type_link', 'series_type_change_link', 10, 2);

function episodes_series_cpt_generating_rule($wp_rewrite) {
    $series_rules = array();

    $series_terms = get_terms( array(
        'taxonomy' => 'series',
        'hide_empty' => false,
    ) );

    $post_type = 'episodes';

    foreach ($series_terms as $series_term) {
        $series_rules['bctv/'.$series_term->slug.'/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&' . $post_type . '=$matches[1]&name=$matches[1]';
    }    

    // merge with global rules
    $wp_rewrite->rules = $series_rules + $wp_rewrite->rules;
}

add_filter('generate_rewrite_rules', 'episodes_series_cpt_generating_rule');


//Updates the View Episode link in admin
function series_change_link( $permalink, $post ) {

    if( $post->post_type == 'episodes' ) {
        $episode_series_terms = get_the_terms( $post, 'series' );
        $term_series_slug = '';
        if( ! empty( $episode_series_terms ) ) {
            foreach ( $episode_series_terms as $term_series ) {
                $term_series_slug = $term_series->slug;
            }
        }        

    $permalink = get_home_url() ."/bctv/" . $term_series_slug . '/' . $post->post_name;
    }
    return $permalink;
}
add_filter('post_type_link', 'series_change_link', 10, 2);

Update

I was able to fix the duplicated slugs happening in the Admin. This was cause by using the %series%, %series_type% in the "rewrite" => array( 'slug' => 'bctv/%series%'.

Now after some addition testing it looks like Wordpress is seeing my Series Type url rewrite as my series rewrite, so this would be why it's 404ing to the home page. I think?

I updated my rewrite strategy with what I am using now

Your Answer

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

Browse other questions tagged or ask your own question.