Product subcat with duplicate name of a subcat of another cat gets the category name in the slug

Suppose the following categories tree in Woo:

Men (slug men)
-- Shoes (slug shoes / was created first)
Women
-- Shoes (slug shoes-women / was created second)

Why is this happening? Both subcats belong to different categories. Why is the category slug appended to the name of the second duplicate subcat?

And anyway, for whatever reason Autommatic decided to program it that way, I need to change it, as the nature of the e-shop I'm building is full of such duplicate subcat names, as it will sell clothing, and there will be the same subcat and even subsubcat names for many categories (Men, Women, Kids, etc)...

Any insight on this?

Topic hierarchical Wordpress

Category Web


It took me two days to figure this out in the proper way, so I'll add it here for anyone else that may need it in the future.

add_filter('wp_unique_term_slug', 'prevent_cat_suffix', 10, 3);
function prevent_cat_suffix($slug, $term, $original_slug)
{
    if ($term->post_type == 'product' && $term->parent !== 0) {
        return $original_slug;
    }

    return $slug;
}

As you can see in the code, I've narrowed it down to products only, but you can make it suitable for any other post type also by removing the $term->post_type == 'product' && in the code above.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.