How can I link posts to a custom "sub-taxonomy" in WordPress? What I mean by "sub-taxonomy" is better explained with an example:
- I've created a custom taxonomy called "Movies", linked to posts (i.e. when you write a post you select what movie it's related to, like a category)
- I've created a "sub-taxonomy" called "Genre", not linked to any post type
- I've leveraged some 3rd party code to Add extra fields to taxonomies to add the "Genre" taxonomy as a selectable option when creating a "Movie" (i.e. genres are linked to movies). The goal here is that selecting a Movie will automatically reference what genre the movie is in, without the post editor having to select both a movie AND genre on each post (and possibly incorrectly selecting different choices each time)
- This all works, but I haven't figured out how to make the new posts that get published to automatically create a reference to both the movie & the genre (i.e. /movies/the-hangover/ will list the post, but /genre/comedy/ will not)
I'm guessing some sort of callback would need to be implemented to manually add a reference for the genre to the term_relationships table, as well as update post counts (and maybe other places?), but I'm not sure how to do that.
...or is there a better way to solve this whole challenge of linking Genres to Movies?
Thanks!
wp_set_post_terms()
in order to set the proper term for theGenre
taxonomy. You should do this when a post is saved - you can use thesave_post
action hook. – Nikola Ivanov Nikolov