I'm in the process of creating a leveled tutorial structure in WordPress and want to build it so that it sets up the front-end with no code needed from the user.

So far, I have a custom post type called 'tut_page' that has a metabox for custom taxonomy called, 'tut_group' and terms can be added via the taxonomy metabox. For my register_taxonomy, the value for 'hierarchical' has been set to true. So I want levels, however I don't want more than two deep. I know that I can choose how many levels of terms to display in a template, but is there a way where I can just prevent users from creating terms that are deeper than two levels from the taxonomy metabox?

Here's what I have in functions.php for registering the taxonomy:

function create_tutorial_topic() {
        'tut_group', //name of taxonomy
        'tut_post', //object type e.g. our post type
                'labels' => array(
                'name' => _x( 'Tutorial Group', 'taxonomy general name' ),
                'singular_name' => _x( 'Tutorial Groups', 'taxonomy singular name' ),
                'search_items' =>  __( 'Search Tutorial Groups' ),
                'all_items' => __( 'All Tutorial Groups' ),
                'parent_item' => __( 'Parent Tutorial Groups' ),
                'parent_item_colon' => __( 'Parent Tutorial Groups:' ),
                'edit_item' => __( 'Edit Tutorial Group' ),
                'update_item' => __( 'Update Tutorial Group' ),
                'add_new_item' => __( 'Add Tutorial Group' ),
                'new_item_name' => __( 'New Tutorial Group' ),
                'menu_name' => __( 'Tutorial Group' ),
            'rewrite' => array( 'slug' => 'tutorial-group' ),
            'hierarchical' => true, //this is true but I only want two levels deep

I would also like to build as much this as much as I can myself without having to use a plugin. I tried searching on this site and found questions about limiting the number of terms, but not hierarchy levels. Thanks for any tips in advance!


Unfortunately, this is not possible, at least not safe.

The function wp_insert_term( $term, $taxonomy, $args = array() ) that handles the creation of taxonomies provides a few filters and hooks, but the only one before the creation of the term in the database is

$term = apply_filters( 'pre_insert_term', $term, $taxonomy );

As you can see, the $args provided in the creation of the term are not passed to the filter - so you have no way of checking how many levels of the taxonomy exists by looking up $args['parent'].

An ugly way of doing it would be after the term is created, hooking into the action created_term or create_term, and checking it there. I have never done this myself, and you may open up a big can of worms deleting the term after creation with no possibility of returning an error afterwards.

If the filter pre_insert_term would accept the $args as an argument, it would me no problem.

  • Thanks for answering. I had a feeling this might not be possible, but I figured I'd ask in case. I was looking at the codex.wordpress.org/Function_Reference/wp_insert_term and I noticed the parent=> in the $args array - all this does is assign whether or not the term is a parent, right? And as you were saying, doesn't differentiate between parent versus grandparent, for example? – RachieVee Jan 30 '14 at 20:07
  • All the $args['parent'] does is define if the term to be added has a parent, nothing else. It does not have anything to do with the function itself. No differentiation, sorry. – fischi Jan 30 '14 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.