1
votes

I am working on a page and I just ran into a problem that I can't solve so I was hoping that somebody could help me out.

Let me illustrate my problem with a random example because I'm having a hard time explaining it.

So I created a custom 'movies' post type. I created a bunch of movies in my WP admin and I can access them by visiting domain.com/movies/moviename.

Now what if I want to separate the movies by their Genre. So I have Comedy, Action, Thriller. I want to have a page that showcases all the movies of one genre and each movie I want to be accessible through domain.com/movies/genre/moviename.

How do I get this structure? Is it somehow possible to create a sub-post type for each Genre?

Any help is appreciated!

1

1 Answers

0
votes

What you're probably looking for is adding taxonomies to your custom post type: https://codex.wordpress.org/Function_Reference/register_taxonomy

Here's an example usage. Assuming your Genres will be hierarchical, meaning, they can be nested. These are equivalent to Categories in Posts.(e.g. Thriller > Action, Thriller > Horror)

The second part is for a non-hierarchical taxonomy, meaning, it cannot be nested. These are equivalent to Tags in Posts. (e.g. #Steven Spielberg)

// hook into the init action and call create_movie_taxonomies when it fires
add_action( 'init', 'create_movie_taxonomies', 0 );

// create two taxonomies, genres and directors for the post type "movie"
function create_movie_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Genres', 'taxonomy general name', 'textdomain' ),
        'singular_name'     => _x( 'Genre', 'taxonomy singular name', 'textdomain' ),
        'search_items'      => __( 'Search Genres', 'textdomain' ),
        'all_items'         => __( 'All Genres', 'textdomain' ),
        'parent_item'       => __( 'Parent Genre', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Genre:', 'textdomain' ),
        'edit_item'         => __( 'Edit Genre', 'textdomain' ),
        'update_item'       => __( 'Update Genre', 'textdomain' ),
        'add_new_item'      => __( 'Add New Genre', 'textdomain' ),
        'new_item_name'     => __( 'New Genre Name', 'textdomain' ),
        'menu_name'         => __( 'Genre', 'textdomain' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'genre' ),
    );

    register_taxonomy( 'genre', array( 'movie' ), $args );

    // Add new taxonomy, NOT hierarchical (like tags)
    $labels = array(
        'name'                       => _x( 'Directors', 'taxonomy general name', 'textdomain' ),
        'singular_name'              => _x( 'Director', 'taxonomy singular name', 'textdomain' ),
        'search_items'               => __( 'Search Directors', 'textdomain' ),
        'popular_items'              => __( 'Popular Directors', 'textdomain' ),
        'all_items'                  => __( 'All Directors', 'textdomain' ),
        'parent_item'                => null,
        'parent_item_colon'          => null,
        'edit_item'                  => __( 'Edit Director', 'textdomain' ),
        'update_item'                => __( 'Update Director', 'textdomain' ),
        'add_new_item'               => __( 'Add New Director', 'textdomain' ),
        'new_item_name'              => __( 'New Director Name', 'textdomain' ),
        'separate_items_with_commas' => __( 'Separate directors with commas', 'textdomain' ),
        'add_or_remove_items'        => __( 'Add or remove directors', 'textdomain' ),
        'choose_from_most_used'      => __( 'Choose from the most used directors', 'textdomain' ),
        'not_found'                  => __( 'No directors found.', 'textdomain' ),
        'menu_name'                  => __( 'Directors', 'textdomain' ),
    );

    $args = array(
        'hierarchical'          => false,
        'labels'                => $labels,
        'show_ui'               => true,
        'show_admin_column'     => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var'             => true,
        'rewrite'               => array( 'slug' => 'director' ),
    );

    register_taxonomy( 'director', 'movie', $args );
}