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'd like to have a non-hierarchical custom taxonomy displayed in the add/edit posts admin screen for a custom post type. Obviously, when the custom taxonomy is non-hierarchical, the meta box that's displayed is similar to the tags - it's a text field with the auto-suggest.

However, I'm more interested in having a flat, non-hierarchical taxonomy display as a list of checkbox terms. In essence, I'd like it's functionality to be the same as categories, with the exception that child terms could not be added.

Surely this is something that other developers have done, but the normal Google searches haven't returned much for me. Apparently this was the default behavior when custom taxonomies were originally introduced in v2.8.

Example of non-hierarchical taxonomy with checkboxes

I'm really not looking for a plugin solution, however code examples from plugins would be fine. When developing sites for client's it is often beneficial to allow them to see the list of pre-established terms for a certain post type. The customer should be allowed to add additional terms, just not additional child terms.

share|improve this question
    
i made a plugin recently that turned taxonomies into radio buttons: wordpress.org/extend/plugins/radio-buttons-for-taxonomies might be worth taking a look at... could probably adapt it to checkboxes. though radios might even be useful since i doubt an item would be both Mac OS AND Ubuntu for instance.... –  helgatheviking Jul 17 '12 at 15:59
add comment

4 Answers

up vote 2 down vote accepted

I'm not sure, if you already found a solution for this, but when I searched for a similar one yesterday, I found this tutorial on WPtuts very helpful. It uses radio buttons, but you can easily modify it to get it working with checkboxes as well. http://wp.tutsplus.com/tutorials/creative-coding/how-to-use-radio-buttons-with-taxonomies/

share|improve this answer
    
Thanks Alex! This is exactly the info I was seeking. –  manifestphil Sep 5 '12 at 14:27
add comment

I got the solution (based on the tutsplus tutorial link above) that deals with actual checkboxes here to work: http://wordpress.org/support/topic/display-tag-admin-box-like-categories-without-hierarchy it ignores the "most used" tab of the metabox that the tutsplus link addresses but personally I'm keeping it simple.

share|improve this answer
    
This is really a simple but effective approach. I strongly suggest following this link and taking a look at this solution. –  Nobita May 30 '13 at 14:01
add comment

I would do that:

$add_action('add_meta_boxes_my_posttype', 'my_add_meta_boxes');

function my_add_meta_boxes ($post) {

    $taxoms = array('my_taxonomy', 'my_second_taxonomy');

    foreach ( get_object_taxonomies( $post ) as $tax_name ) {
        if( !in_array($tax_name, $taxoms) ) continue;

        $taxonomy = get_taxonomy($tax_name);
        if ( ! $taxonomy->show_ui )
            continue;

        $label = $taxonomy->labels->name;

        if ( !is_taxonomy_hierarchical($tax_name) ) {
            add_meta_box($tax_name . 'div', $label, 'post_categories_meta_box', null, 'side', 'core', array( 'taxonomy' => $tax_name ));
            remove_meta_box('tagsdiv-' . $tax_name, null, 'side');
        }
    }
}

After, up to you to hide the parent select box of the form

share|improve this answer
add comment

Pardon my french, but why not use the built in hierarchial=true and after that hide all the Parent dropboxes by using admin css ?

share|improve this answer
add comment

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.