Multiple custom post types with single taxonomy URL rewrite

46 Views Asked by At

I've created 2 custom post types (let's call them "cpt-1" and "cpt-2").

These two CPTs share the same custom taxonomy (called "category").

I've done this with the ACF plugin.

Here's the default URL structure:

  • Single post (cpt 1): example.com/cpt-1/post-name
  • Single post (cpt 2): example.com/cpt-2/post-name
  • Taxonomy archive: example.com/category/category-name/subcategory-name

Here's what I need:

  • Single post (cpt 1): example.com/cpt-1/category-name/post-name
  • Single post (cpt 2): example.com/cpt-2/category-name/post-name

This should also work if a single post is associated with a sub-category, for example, example.com/cpt-1/category-name/subcategory-name/post-name

  • Taxonomy archive (showing cpt 1 posts only): example.com/cpt-1/category-name/
  • Taxonomy archive (showing cpt 2 posts only) : example.com/cpt-2/category-name/

This should also work for sub-categories, for example example.com/cpt-1/category-name/subcategory-name

Is it possible to do this?

I've already consulted dozens of topics and articles. The simplest and most concise solution seems to be this: Wordpress URL Rewrite to sync custom taxonomy and post type

I also find a code that manage hierachical taxonomy (category and subcategories) : https://gist.githubusercontent.com/igorbenic/3ba41de1db69bf028651e98b4615cfee/raw/c38bc462bbcfb21ebd1784ede59c5841767f5eb3/request2.php

From what I understand, a custom rewrite rule must be used to restructure URLs, and a post_type_link filter to display new URLs, but I don't have a good enough level of coding to adapt it to my case.

Here are the features I'm having trouble finding:

  • Taxonomy hierarchy management in the URL (example.com/cpt/category/subcategory/subsubcategory/post-name)
  • Use the same taxonomy archive for 2 different custom posts (example.com/cpt-1/category AND example.com/cpt-2/category). Besides, I don't even know if it's possible to separate the taxonomy archive according to the CPT.

If it's too complex, I'm tempted not to use taxonomy, and to create a page hierarchy for each CPT to have the URL structure I want easily (for example, inside a custom post type, create a "category-name" single post as parent page, and a "post-name" child single post). What do you think about it?

Thanks in advance :)

0

There are 0 best solutions below