Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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() {
    register_taxonomy(
        'tut_group', //name of taxonomy
        'tut_post', //object type e.g. our post type
        array(
                '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!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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 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 at 23:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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