How to build overlapping custom post_type and taxonomy rewrites without conflicts?

by Stratboy   Last Updated August 03, 2017 14:08 PM

custom post type: gallery

taxonomy: gallery_type (ex terms: photo, video)

wanted urls:

domain.com/gallery/ > all 'gallery' custom posts

domain.com/gallery/video > all 'gallery' custom posts belonging to 'video' term

domain.com/gallery/video/my-fabulous-gallery-1 > the 'gallery' custom post called 'My Fabulous  Gallery 1'

unwanted urls:

domain.com/gallery/my-fabulous-gallery-1

I think this is quite a common problem. In the past I already solved it in various ways with the use of add_rewrite_rule() and some regexp astro-magics. Ok.

BUT:

  • Since register_post_type/taxonomy do have a rewrite option (which I usually use for simple things), I wonder: are there more elegant and less involved ways to achieve the results I want?

  • Also, after changing the rules/routes/rewrites, I'd like to have wordpress to correctly generate links and things like that, using its standard functions (get_the_permalink() etc...). How to?


There's an old and quite good article out there:

https://code.tutsplus.com/articles/the-rewrite-api-post-types-taxonomies--wp-25488

If you take a look to "Manipulating WordPress Rewrites" you'll see they suggest ways to achieve what I want, but they don't work perfectly. I get the first 2 urls, but this one will not work:

domain.com/gallery/video/my-fabulous-gallery-1

it will redirect to the standard one:

domain.com/gallery/my-fabulous-gallery-1


Related Questions




Change CPT permalink to use the category

Updated June 11, 2019 20:08 PM