How to make custom taxonomy check boxes like ‘Categories’

Just set 'hierarchical' => true in the args to register_taxonomy() (even if you never intend to add terms with parent-child relationships) and you will get the same UI for assigning terms from that custom taxonomy when creating/editing that custom post type as you would with the builtin category taxonomy.

And if you want a dropdown of terms on the edit.php screen (like WP Core automatically adds for ‘category’), add the following:

add_action ('restrict_manage_posts', 'add_custom_taxonomy_dropdowns', 10, 2) ;

/**
 * add a dropdown/filter to the edit.php screen for our custom taxonomies
 *
 * @param $post_type string - the post type that is currently being displayed on edit.php screen
 * @param $which string - one of 'top' or 'bottom'
 */
function
add_custom_taxonomy_dropdowns ($post_type, $which="top")
{
    if ('case_study' != $post_type) {
        return ;
        }

    $taxonomies = get_object_taxonomies ($post_type, 'object') ;
    foreach ($taxonomies as $tax_obj) {
        if ($tax_obj->_builtin) {
            // let WP handle the builtin taxonomies
            continue ;
            }

        $args = array (
            'show_option_all' => $tax_obj->labels->all_items,
            'taxonomy'    => $tax_obj->name,
            'name'        => $tax_obj->name,
            'value_field' => 'slug',
            'orderby'     => 'name',
            'selected'    => isset ($_REQUEST[$tax_obj->name]) ? $_REQUEST[$tax_obj->name] : '0',
            'hierarchical'    => $tax_obj->hierarchical,
            'show_count'      => true,
            'hide_empty'      => true,
            'fields' => 'all',
            ) ;

        wp_dropdown_categories ($args) ;
        }

    return ;
}

Leave a Comment