Custom Taxonomy rewrite rule working for everything other than singles

Question

I have been writing a custom rewrite for a CPT, its all working other than the cpt singles.
Its really odd as the permalink is correct when you edit a post. However when you navigate to link it is not?

Post Type declaration

    function bike_post_init()
{   
    $menu_icon_url = get_theme_file_path('assets/dist/img/misc/bike.svg');
    $bike_icon = file_get_contents($menu_icon_url);
    $singular="Bike";
    $plural="Bikes";
    $labels = array(
        'name'               => __($plural),
        'singular_name'      => __($singular),
        'add_new'            => __('Add New ' . $singular),
        'add_new_item'       => __('Create New ' . $singular),
        'edit'               => __('Edit ' . $singular),
        'edit_item'          => __('Edit ' . $singular),
        'new_item'           => __('New ' . $singular),
        'view'               => __('View ' . $plural),
        'view_item'          => __('View ' . $singular),
        'search_items'       => __('Search ' . $plural),
        'not_found'          => __('No ' . $plural .' found'),
        'not_found_in_trash' => __('No ' . $plural . ' found in trash'),
        'parent'             => __('Parent ' . $singular),
        'featured_image'        => _x($singular . ' Cover Image', 'Overrides the “Featured Image” phrase for this post type.', $singular),
        'set_featured_image'    => __('Set ' . $singular .' Cover Image', $singular),
    );

    $args = array(
        'labels'                => $labels,
        'description'           => __('This is where you can create new '. $plural .' on your site.'),
        'public'                => true,
        'publicly_queryable'    => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'query_var'             => true,
        'rewrite'               => array('slug' => 'bikes/%bike_cat%', 'hierarchical' => true, 'with_front' => false),
        'capability_type'       => 'post',
        'has_archive'           => false,
        'exclude_from_search'   => false,
        // 'menu_position'         => 2,
        'menu_icon'             => 'data:image/svg+xml;base64,' . base64_encode( $bike_icon ),
        'hierarchical'          => true,
        '_builtin'              => false,
        'show_in_rest'          => true,
        'supports'              => array('title', 'thumbnail', 'custom-fields', 'page-attributes'),
    );
    // REGISTER POST TYPE
    register_post_type($singular, $args);
}

Taxonomy Declaration

function create_bike_taxonomies()
{
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x('Categories', 'taxonomy general name'),
        'singular_name'     => _x('Category', 'taxonomy singular name'),
        'search_items'      =>  __('Search Categories'),
        'all_items'         => __('All Categories'),
        'parent_item'       => __('Parent Categories'),
        'parent_item_colon' => __('Parent Categories:'),
        'edit_item'         => __('Edit Category'),
        'update_item'       => __('Update Category'),
        'add_new_item'      => __('Add New Category'),
        'new_item_name'     => __('New Category Name'),
        'menu_name'         => __('Category'),
    );

    register_taxonomy('bike_cat', array('bike'), array(
        'hierarchical'  => true,
        'labels'        => $labels,
        'show_ui'       => true,
        'show_in_rest' => true,
        'query_var'     => true,
        //'rewrite'     => true,
        'rewrite'       => array('slug' => 'bike','hierarchical' => true),
        'default_term'      => [
            'name' => 'Uncategorised', //(string) Name of default term.
            'slug' => 'uncategorised', //(string) Slug for default term.
            'description' => '', //(string) Description for default term.
        ],
    ));

}
add_action('init', 'create_bike_taxonomies', 0);
register_taxonomy_for_object_type('bike_cat', 'bike');
add_action('admin_init', 'flush_rewrite_rules');

Permalink Rewrite function

add_filter('post_type_link', 'bike_post_type_link', 10, 2);
function bike_post_type_link($link, $post)
{
    if ($post->post_type === 'bike') {
        if ($terms = get_the_terms($post->ID, 'bike_cat')){
            $slug = [];
            foreach ($terms as $term) {
                //Permalink trouble https://developer.wordpress.org/reference/functions/sanitize_title_with_dashes/
                if ($term->parent == 0) {
                    array_unshift($slug, sanitize_title_with_dashes($term->name));
                } else {
                    array_push($slug, sanitize_title_with_dashes($term->name));
                };
            };
            $link = str_replace('%bike_cat%',join("https://wordpress.stackexchange.com/", $slug), $link);
        }else{
            $link = str_replace('%bike_cat%', 'uncategorised', $link);
        }
    }

    return $link;
}

0
Sam Sedgeman 4 weeks 2023-01-02T14:15:33-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse