Creating a non-hierarchical Taxonomy that behaves like categories

enter image description here

I’ve created a custom post-type along with a custom taxonomy and everything is fine, apart from one particular thing.

First off, my use case: I need it to act like a category system, however not be hierarchical (one level only), so I left both to the default setting of false for the hierarchical setting.

This has been fine, apart from one problem where it allows me to add a “sub-level” within the metabox.

First, the code:

register_post_type( 'letter',
                    array(
                        'labels' => array(
                            'name' => __( 'Letters' ),
                            'singular_name' => __( 'Letter' ),
                            //..........
                        ),
                        'public' => true,
                        'taxonomies' => array('letter'),
                        //........
                    )
);

// Our args for the custom taxonomy below
$args = array(
    'labels' => array(
        'name' => __('Recipients'),
        'singular_name' => __('Recipient'),
        //.....
    ),
    'meta_box_cb' => 'post_categories_meta_box',
);

// Register a custom taxonomy for our letter categories
register_taxonomy( 'recipient', 'letter', $args );

// Connect the post type and taxonomy together to be safe
register_taxonomy_for_object_type( 'recipient', 'letter' );

Now, as you can see I needed to set the meta_box_cb to post_categories_meta_box otherwise it would end up having the functionality of the tag metabox, so, now it looks like this:

So this is good, I get the category functionality that I like, however I also get the “Add New Recipient” where when you open it up it allows you to add a new “recipient” and select a parent recipient; this I do not want.

Is my only option to create a new callback function based on the post_categories_meta_box function and remove the respective code to get rid of that parent functionality?

I don’t particularly want to do that as then it would break future updates to this function.

Is there another option?

Read more here:: Creating a non-hierarchical Taxonomy that behaves like categories

Leave a Reply

Your email address will not be published. Required fields are marked *