Wordpress - limit selection of custom taxonomies to one?
i have a php solution for you:
add_filter('wp_terms_checklist_args', 'htmlandcms_select_one_category');
function htmlandcms_select_one_category($args) {
if (isset($args['taxonomy']) && $args['taxonomy'] == 'category_portfolio') {
$args['walker'] = new Walker_Category_Radios;
$args['checked_ontop'] = false;
}
return $args;
}
class Walker_Category_Radios extends Walker {
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent<ul class='children'>\n";
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
function start_el( &$output, $category, $depth, $args, $id = 0 ) {
extract($args);
if ( empty($taxonomy) )
$taxonomy = 'category';
if ( $taxonomy == 'category' )
$name = 'post_category';
else
$name = 'tax_input['.$taxonomy.']';
/** @var $popular_cats */
$class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
/** @var $selected_cats */
$output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="radio" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), TRUE, FALSE ) . disabled( empty( $args['disabled'] ), FALSE, FALSE ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';
}
function end_el( &$output, $category, $depth = 0, $args = array() ) {
$output .= "</li>\n";
}
}
Instead of hacking it with jQuery, a more reliable solution would be to replace the meta box with your own, in PHP.
Anyway, the problem is most likely with the '[' and ']' characters in the selector:
"input[name=tax_input[ba_locations][]]"
could be rewritten as
"input[name=tax_input\\[ba_locations\\]\\[\\]]"
See https://stackoverflow.com/questions/2786538/need-to-escape-a-special-character-in-a-jquery-selector-string