Grouping categories by genre

It sounds to me like genre should be its own taxonomy. Then posts get assigned to both categories and genres. If you do it this way, here’s what you need to do to get everything working:

First, register the taxonomy genre.

Next, assuming you’re using the core posts and categories, go to Settings → Permalinks. Set your permalinks to be /genre/%genre%/%category%/%postname%'/ and your category base to be /genre/all/.

Lastly, you’ll want to add a filter to post_link so the auto-generated links don’t contain the literal string “%genre%”:

function wpse_101766_post_type_link( $permalink, $post ) {
    if ( 'post' == $post->post_type ) {
        $genre="all";
        if ( false !== strpos( $permalink, '%genre%' ) ) {
            $first_genre = wp_get_object_terms( array( $post->ID ), 'genre', array( 'orderby' => 'term_id', 'count' => 1 ) );
            if ( ! is_wp_error( $first_genre ) && !empty( $first_genre ) ) {
                $first_genre = array_shift( $first_genre );
                $genre = $first_genre->slug;
            }
        }
        return str_replace( '%genre%', $genre, $permalink );
    }

    return $permalink;
}
add_filter( 'post_link', 'wpse_101766_post_type_link', 10, 2 );

As always when making rewrite changes, be sure to flush your rewrite rules. You did this when you saved your changes on the Permalinks page, but if you made changes after that, go back and click “Save Changes” again.

Now, the URLS look like this: (using the genre ‘action’, category ‘fantasy’, and post ‘lord-of-the-rings’:

  • /genre/action/
  • /genre/action/fantasy/
  • /genre/action/fantasy/lord-of-the-rings/

You can also go to:

  • /genre/all/fantasy/ to list all posts in the category fantasy across any genre
  • /genre/action/fantasy,anime/ to list all posts in the genre “action” and in either categories “fantasy” or “anime”
  • /genre/action/fantasy+anime/ to list all posts in the genre “action” and in both categories “fantasy” or “anime”
  • /genre/action,adventure/fantasy+anime/ to list all posts in the genre “action” or the genre “adventure” and in both categories “fantasy” or “anime”

… and so on. It gives you a lot of flexibility!

To list categories by genre, create a taxonomy-genre.php file in your theme. Here’s a function to get all categories “within” a genre, just call it like so:

$category_ids = get_terms_by_overlap( array(
    'term_ids' => get_current_object_id(),
    'get_taxonomy' => 'category',
    'through_taxonomy' => 'genre'
) );

You can then loop over this however you’d like. Here’s a basic example:

<ul>
    <?php foreach ( $category_ids as $category_id ) : ?>
    <li><a href="https://wordpress.stackexchange.com/questions/101766/<?php echo get_category_link( $category_id ) ?>"><?php echo get_the_category_by_ID( $category_id ) ?></a></li>
    <?php endforeach ?>
</ul>