Skip to content

Instantly share code, notes, and snippets.

@gschoppe
Created February 28, 2016 01:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gschoppe/29ba81a1f676d7802cb8 to your computer and use it in GitHub Desktop.
Save gschoppe/29ba81a1f676d7802cb8 to your computer and use it in GitHub Desktop.
Use category (checkbox-based) interface with non-hierarchical custom taxonomies
<?php
// Event taxonomies
add_action( 'init', function() {
$labels = array(
'name' => _x( 'Terms', 'taxonomy general name' ),
'singular_name' => _x( 'Term', 'taxonomy singular name' ),
);
register_taxonomy( 'taxonomy_name', array( 'post' ), array(
'hierarchical' => false,
'labels' => $labels,
'meta_box_cb' => "post_categories_meta_box",
) );
} );
add_action( 'admin_head', function() {
?>
<style type="text/css">
#newtaxonomy_name_parent {
display: none;
}
</style>
<?php
});
add_action( 'admin_init', function() {
if( isset( $_POST['tax_input'] ) && is_array( $_POST['tax_input'] ) ) {
$new_tax_input = array();
foreach( $_POST['tax_input'] as $tax => $terms) {
if( is_array( $terms ) ) {
$taxonomy = get_taxonomy( $tax );
if( !$taxonomy->hierarchical ) {
$terms = array_map( 'intval', array_filter( $terms ) );
}
}
$new_tax_input[$tax] = $terms;
}
$_POST['tax_input'] = $new_tax_input;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment