5

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.

3

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/

4

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.

  • 1
    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
  • Its working... Its simple – Mansoorkhan Cherupuzha Mar 18 '15 at 12:07
2

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

  • This is excellent, eg use #newMYTAX_parent, .taxonomy-MYTAX .term-parent-wrap { display:none; }. – bonger Feb 18 '15 at 5:26
1

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

1

use hierarchy and hide the parent selects, like this:

function hide_taxonomy_parent( $slug ) {
    add_action( 'admin_head', function () use ( $slug ) {
        echo "<style>
            .taxonomy-{$slug} .term-parent-wrap,
            [for='new{$slug}_parent'],
            #new{$slug}_parent {
                display: none !important;
                visibility: hidden !important;
            }
        </style>";
    } );
}

hide_taxonomy_parent( 'brand' );
-1

My solution:

function custom_register_taxonomy_args( $args, $name, $object_type ) {
    global $pagenow;

    if ( $name == 'product_cat' && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) ) {
        $args['hierarchical'] = true;
    }

    return $args;
}
add_filter( 'register_taxonomy_args', 'custom_register_taxonomy_args', 10, 6 );
  • This is not what the questioner wanted. As this will make that particular taxonomy behave like a hierarchical taxonomy & it'll still have some glitches when user tries to save a new taxonomy with hierarchy. – Fayaz Mar 16 '17 at 7:44

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.