There have been many times we have wanted to use taxonomies as a 1-to-1 ratio with a post or custom post types. This is arguably more effective than using metadata because as Alex King pointed out, using a taxonomy query is more efficient than using a meta query, not to mention all the other cool things that are done by default with custom taxonomies (i.e. admin columns).
This snippet replaces the default taxonomy checkbox select metabox for hierarchical taxonomies with a radio select metabox, which effectively limits your taxonomy selection to one term. Just include this file from your functions.php or plugin file and update the parameters at the bottom.
Note: A radio select metabox will get out of hand quickly if more than a few terms are needed.
Update: Helen Housandi was kind enough to look over our snippet and provide some feedback. Thanks to that, there are some pretty awesome improvements to the class.
Update 2: Code is now on Github. Forks/pull requests welcome.
[snippet slug=taxonomy-radio-metabox]
View or fork on github.
Your code within code.webdevstudios.com has a problem with:
&&it converted the first ‘&’ into its entity number:
&&Also, your code is incorrect here:
public function add_radio_box() {
foreach ( $this->post_types() as $key => $cpt ) {
// remove default category type metabox
remove_meta_box( $this->slug .'div', $cpt, 'core' );
// remove default tag type metabox
remove_meta_box( 'tagsdiv-'.$this->slug, $cpt, 'core' );
// add our custom radio box
add_meta_box( $this->slug .'_radio', $this->metabox_title(), array( $this, 'radio_box' ), $cpt, $this->context, $this->priority );
}
}
it should be
public function add_radio_box() {
foreach ( $this->post_types() as $key => $cpt ) {
// remove default category type metabox
remove_meta_box( $this->slug .'div', $cpt, 'side' );
// remove default tag type metabox
remove_meta_box( 'tagsdiv-'.$this->slug, $cpt, 'side' );
// add our custom radio box
add_meta_box( $this->slug .'_radio', $this->metabox_title(), array( $this, 'radio_box' ), $cpt, $this->context, $this->priority );
}
}
notice how I’ve changed the last variable on each remove_meta_box from ‘core’ to ‘side’
Thanks Philip. Code has been updated, and code.wds link replaced with github. Pull requests FTW!
Pretty cool!
Personally tho, I’ve been using jquery added to admin_head to change the input type to radio buttons
jQuery("#categorychecklist>li>label input").each(function(){
this.type = "radio";
});
Specify the ID’s of each UL (separate ULs for each tab of a tax; All/Most Used) and its tag (input) for each list of tax terms that you want to change to radio buttons
An alternative approach is to use javascript to replace the checkboxes with radios. Here’s an example (using JQuery)
$('form#post #categorychecklist input[type=checkbox]').each(function(){
var value = $(this).val();
var id = $(this).attr('id');
var name = $(this).attr('name');
var checked = '';
if( $(this).is(':checked') ){
checked = ' checked="checked"';
}
$(this).replaceWith('');
});
Now this is a sweet class!
You overthink it, just use JS to change the input type to radio, done, 3 lines.
why my custom field not show, i add login if category == selected then show custom custom field
Hi, Samsul. Thank you for your comment. We have a GitHub for Taxonomy Single Term where you can submit an issue. Thank you. https://github.com/WebDevStudios/Taxonomy_Single_Term