Custom Taxonomy Rewrite redirects back to home page

by CodeWeaver   Last Updated June 12, 2019 15:08 PM

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

function episodes_series_type_cpt_generating_rule($wp_rewrite) {
    $rules = array();

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

    $post_type = 'episodes';

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

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

add_filter('generate_rewrite_rules', 'episodes_series_type_cpt_generating_rule');

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

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

    $post_type = 'episodes';

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

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

add_filter('generate_rewrite_rules', 'episodes_series_cpt_generating_rule');

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

    if( $post->post_type == 'episodes' ) {
        $episode_terms = get_the_terms( $post, 'series' );
        $term_slug = '';
        if( ! empty( $episode_terms ) ) {
            foreach ( $episode_terms as $term ) {
                $term_slug = $term->slug;
            }
        }

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

add_filter('post_type_link', 'episode_series_type_permalink_structure', 10, 4);
function episode_series_type_permalink_structure($post_link, $post, $leavename, $sample) {
    if( $post->post_type == 'episodes' ) {
        if (false !== strpos($post_link, '%series_type%')) {
            $series_type_term = get_the_terms($post->ID, 'series_type');

            if (!empty($series_type_term))
                $post_link = str_replace('%series_type%', array_pop($series_type_term)->slug, $post_link);
            else

            foreach ( $series_type_term as $term ) {
                $term_slug = $term->slug;

                return $term_slug;
            }
        }
    }
    return $post_link;
}

add_filter('post_type_link', 'episode_series_permalink_structure', 11, 4);
function episode_series_permalink_structure($post_link, $post, $leavename, $sample) {
    if( $post->post_type == 'episodes' ) {
        if (false !== strpos($post_link, '%series%')) {
            $series_term = get_the_terms($post->ID, 'series');

            if (!empty($series_term))
                $post_link = str_replace('%series%', array_pop($series_term)->slug, $post_link);
            else

            foreach ( $series_term as $term ) {
                $term_slug = $term->slug;

                return $term_slug;
            }
        }
    }
    return $post_link;
}


Related Questions





Change CPT permalink to use the category

Updated June 11, 2019 20:08 PM

Customize Custom Post Type URL

Updated September 16, 2018 21:08 PM