Custom post type with specific category structure

I have successfully created a custom post type (aka “Instruction”) and two different categories (aka “Program” and “Exercise”). An “Instruction” needs to be filed under an “Exercise” and an Exercise needs to have a “Program” as a parent category.

This is how I have registered the taxonomies:

    function create_taxonomy() {
        $programLabels = array(
            'name' => _x( 'Programs', 'taxonomy general name' ),
            'singular_name' =>_x( 'Program', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Programs' ),
            'popular_items' => __( 'Popular Programs' ),
            'all_items' => __( 'All Programs' ),
            'edit_item' => __( 'Edit Program' ),
            'update_item' => __( 'Update Program' ),
            'add_new_item' => __( 'Add New Program' ),
            'new_item_name' => __( 'New Program' ),
            'separate_items_with_commas' => __( 'Separate programs with commas' ),
            'add_or_remove_items' => __( 'Add or remove programs' ),
            'choose_from_most_used' => __( 'Choose from the most used programs' )
        );
        register_taxonomy(
            'category_program', // The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
            'cus_instruction',      // post type name
            array(
                'hierarchical'      => false,
                'label'             => __('Program'),
                'labels'            => $programLabels,
                'query_var'         => true,
                'rewrite'           => array(
                    'slug'          => 'program',    // This controls the base slug that will display before each term
                    'with_front'    => false        // Don't display the category base before
                )
            )
        );

        $exerciseLabels = array(
            'name' => _x( 'Exercises', 'taxonomy general name' ),
            'singular_name' =>_x( 'Exercise', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Exercises' ),
            'popular_items' => __( 'Popular Exercises' ),
            'all_items' => __( 'All Exercises' ),
            'parent_item' =>  __( 'Program' ),
            'parent_item_colon' => ( 'Program:' ),
            'edit_item' => __( 'Edit Exercise' ),
            'update_item' => __( 'Update Exercise' ),
            'add_new_item' => __( 'Add New Exercise' ),
            'new_item_name' => __( 'New Exercise' ),
            'separate_items_with_commas' => __( 'Separate exercises with commas' ),
            'add_or_remove_items' => __( 'Add or remove exercise' ),
            'choose_from_most_used' => __( 'Choose from the most used exercises' ),
            'menu_name' => __( 'Exercises' )
        );
        register_taxonomy(
            'category_exercise',   // The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
            'cus_instruction',          // post type name
            array(
                'hierarchical'      => true,
                'label'             => __('Exercise'),
                'labels'            => $exerciseLabels,
                'query_var'         => true,
                'rewrite'           => array(
                    'slug'          => 'exercise',    // This controls the base slug that will display before each term
                    'with_front'    => false          // Don't display the category base before
                )
            )
        );
    }
    add_action( 'init', 'create_taxonomy', 0);

Conceptually setting the “cus_instruction” as the post type for the category “Program” is not correct but that was the only way I could make WordPress display it in the admin sidebar.
Setting the ‘parent_item’ in the “Exercise” category doesn’t have the effect of binding the category “Program” as a parent though, it only changes the label of the parent category.

Is my requirement possible in WordPress? And if so, how do I set up the category structure?

1 Answer
1

I think you’ve arrived at the structure that WP doesn’t quite support.

Let’s start with following premises:

  1. Taxonomies exist for grouping posts.
  2. Hierarchical taxonomies can have multiple level of grouping (with larger groups encompassing smaller groups).

But what you had drafted is something else — taxonomy grouping another taxonomy. Simply put that’s not in the plan. 🙂

It’s a little hard to advice without seeing actual data, but my guess would be that if Program/Exercise are taxonomies and need to have nested relationship then they likely just need to be one hierarchical taxonomy.

Leave a Comment