How to make a term belong to multiple taxonomies?
-
I’m sorry if this topic comes out pretty often or I come out a little offensive, just had frustrated hacking session with WP Core.
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.
- The topic ‘How to make a term belong to multiple taxonomies?’ is closed to new replies.