As the title says, I’d like to implement a system where I’d like to have a term belonging to multiple taxonomies.

Just in case someone’s wondering “what on earth does he need this for?”, hear me out. I’m trying to implement a UML specification of an entity called “Person” who can act as a speaker, author, interviewer for the posts and custom post types we design. The idea is to be able to tag them just like you add categories/tags, query them in wp-admin, etc.

So the idea I came up with was to create a term for each “Person”, extend with custom term metas and then make this term part of taxonomies speaker, author and interviewer. Upon reading the DB schema of WP, I was able to make the mental map of how things would work. As you can see in the schema, the table wp_terms has one to many relationship with wp_term_taxonomy, meaning that a term can be part of multiple taxonomies.

However, when I encountered how the codebase for WP was implemented, I was surprised to find that the WP_Term class implementation throws an error when the term ID is shared across multiple taxonomies. Here’s the code for reference: https://github.com/WordPress/wordpress-develop/blob/6.2/src/wp-includes/class-wp-term.php#L153-L162 As you can see, the class is set to final, so it’s not possible to extend it either.

This makes me wonder, what was the point of separating the wp_terms and wp_term_taxonomy table in the first place? We could’ve merged them together, instead of creating joins for slugs. As far as I can tell, the design contradicts with the implementation. Has it been overlooked or has it been known, but an obvious choice was made for legacy reasons?

This, of course, can be a misinterpretation of the things, I am eager to understand if I am mistaken.

And lastly, what are my best bet for the scenario I’m working on? The other option is that I create 3 different term instances for three type of taxonomies, and make sure that CRUD operations are atomic in nature. My gut says try to avoid it, but if there’s no other way, I’d have to go with that.

  • Have you seen this article? It explains terms and taxonomy relationships really well: code.tutsplus.com/tutorials/…
    – Lisa
    May 25, 2023 at 6:52
  • @Lisa I don't know if I missed anything but I read "but in some cases you will have more than one record for each term. This happens when you create two terms with the same name and slug in different taxonomies, and means that you could create a query to output posts with that term in multiple taxonomies." I gave it a try with 3 different types of taxonomies. Each time I created a term with the same name and slug, a new row in the wp_terms is created. But as per this article, one row should be created, no? Or am I misinterpreting again? May 25, 2023 at 12:33


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.