Allow duplicate slugs for posts on Custom post type in different categories/taxonomy?

i'm making a photo gallery for a plastic surgery website, with before and after photos of patients. i made a custom post type /photo-gallery and a taxonomy /procedures, after reading this post i was able to include the taxonomy in the permalink, now i want to be able to generate permalinks like

www.site.com/photo-gallery/facelift/patient-1
www.site.com/photo-gallery/liposuction/patient-1
www.site.com/photo-gallery/breast-augmentation/patient-1

But instead i got a permalink like.

www.site.com/photo-gallery/facelift/patient-1
www.site.com/photo-gallery/liposuction/patient-1-2
www.site.com/photo-gallery/breast-augmentation/patient-1-3

is there a way to achieve permalinks like in the first group?

Topic custom-taxonomy custom-post-types Wordpress

Category Web


A simple solution would be to use a single hierarchical post type instead of a taxonomy/non-hierarchical post type combination. A hierarchical post type allows you to assign a parent post to each post.

For an example you can test immediately, the built-in post post type in WordPress is non-hierarchical, the built-in page post type is hierarchical.

You can make a custom post type hierarchical by setting the hierarchical argument to true when you register the post type. You also need to add page-attributes to the supports argument when you register your post type, so that you will have the meta box that allows you to select a parent for each post.

In this scheme, you will first create custom posts for each of your categories, facelift, liposuction, etc.. You can then create the individual child patient-1 posts and assign the appropriate parent to each.

WordPress will allow these posts to have non-unique slugs because each will have a unique parent. This works because "behind the scenes" these posts are being queried by the full parent/child path, so WP is able to differentiate the ones with non-unique slugs.

About

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