Right now I have a custom taxonomy for my custom post type case_study
. Everything is working just find except for the fact that when creating a new post and adding a Taxonomy, it’s “tags” and i was looking to do what Posts do with their category (the checkbox). Is there anyway I can do that instead of adding these ‘Tags’
Here is my code for registering this taxonomy
PhP
add_action( 'init', 'create_case_study_tax' );
$tax_args = array(
'hierarchical' => false,
'show_ui' => true,
'label' => __('Industry'),
'show_admin_column' => true,
'query_var' => true,
);
function create_case_study_tax() {
register_taxonomy('industry', 'case_study', $tax_args);
}
1 Answer
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 ;
}