0
votes

How can I link posts to a custom "sub-taxonomy" in WordPress? What I mean by "sub-taxonomy" is better explained with an example:

  1. 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)
  2. I've created a "sub-taxonomy" called "Genre", not linked to any post type
  3. 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)
  4. 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!

1
So why don't you link the "Genre" taxonomy to posts as well? Is it even possible not to link to a post type - codex.wordpress.org/Function_Reference/register_taxonomy ?Nikola Ivanov Nikolov
@NikolaIvanovNikolov it is possible not to link it, just give it an empty array or a non-existing posttypejanw
@NikolaIvanovNikolov There are 2 reasons not to link it directly 1) it's an extra redundant step that could be pre-defined, I want it to be faster/easier for editors to post 2) it allows editors to accidentally select two different genres for the same movie on two different posts (for the sake of my definition, this is bad)Steve
So in theory you should be able to use wp_set_post_terms() in order to set the proper term for the Genre taxonomy. You should do this when a post is saved - you can use the save_post action hook.Nikola Ivanov Nikolov

1 Answers

0
votes

Thanks to @NikolaIvanovNokolov for providing the right path for the solution!

// Add action hook to link genre to movie selection
add_action('save_post', 'save_genre');

// Action hook to link Genre taxonomy to the post
function save_genre($post_id) {
    // Remove reference to any old genres
    wp_set_post_terms($post_id, NULL, 'genre');

    // Add genre reference
    $movie_ids = get_the_terms($post_id, 'movie');
    if ($movie_ids && !is_wp_error($movie_ids)) {
        foreach ($movie_ids as $movie_id) {
            $genre_slug = get_tax_meta($movie_id, 'movie_tags_genre');
            if ($genre_slug) {
                $genre = get_term_by('slug', $genre_slug, 'genre');
                wp_set_post_terms($post_id, $genre->term_id, 'genre');
            }
        }
    }
}