Add hierarchical taxonomy to permalink for custom post type

I’ve found various questions about this issue, and I still can’t get this to work.

I have a post type called “product” and a taxonomy called “type”. I’d like the URL for a single product post to be:

mysite.com/product/type/subtype/postname/

By default, it is:

mysite.com/product/postname/

I’ve tried every example I’ve found. Some say all you have to do is set hierarchical=>true for the taxonomy rewrite, but this had no effect.

I’ve tried this plugin: https://wordpress.org/plugins/custom-post-type-permalinks, which allowed me to use /%type%/%postname%/ in the permalink settings for the post type. The URL structure is correct, but I always get a 404 on the single product pages when the posts are in a subcategory. I tried removing the post from the parent taxonomy and only putting them in the subcategory — still 404. The permalink works only if the product post is assigned to the parent category only. The author provides no support on the forum. (Yes I remembered to flush the permalinks.)

So I’ve come here hoping someone can finally provide a working solution (plugin or no plugin).

Here is my post type and taxonomy code:

// post type
register_post_type( 'product',
        array(
            'labels' => $labels,
            'public' => true,
            'hierarchical'  => false,
            'has_archive' =>false,
            'rewrite' => array( 'slug' => 'product', 'with_front' => false ),
            'query_var' => true,
            'supports' => array( 'title', 'editor', 'thumbnail' )
            )
        );

// taxonomy
register_taxonomy( 'type', 'product',
    array(
        'labels' => $labels,
        'hierarchical' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'rewrite' => array( 'hierarchical' => true, 'with_front' => false ),
    ) 
);

What can I do to achieve this? Thank you!

0

Leave a Comment