7

Wordpress allows Admin to select more than one category for a post , I would like to change that so user is allowed to select only 1 category for a custom post type.

Please note I cannot remove category selection box due to requirement of the project. Any help appreciated

3
  • 1
    I had to do that once, so I wrote a plugin called Radio Buttons for Taxonomies that will restrict a taxonomy to only 1 term. Commented May 7, 2015 at 13:21
  • 1
    The solution I have been using for a little while now, is to use js to change the taxonomy checkboxes to radio buttons. It works, but this seems like one of those options that should've been built-in to wordpress.
    – gdaniel
    Commented May 13, 2015 at 23:50
  • @gdaniel could you share how are you doing this?
    – June
    Commented May 15, 2015 at 6:54

1 Answer 1

9

Here's the JS I use to replace checkboxes for radio buttons.

function checktoradio(){
    echo '<script type="text/javascript">jQuery("#categorychecklist-pop input, #categorychecklist input, .cat-checklist input").each(function(){this.type="radio"});</script>';
}

add_action('admin_footer', 'checktoradio');

This would also work for custom terms, you just need to find out the custom term ID used by inspecting the metabox element.

You would place this code inside functions.php. It basically adds a jquery script to the footer of the admin pages.

Disclaimer: I found the original code a long time ago on some website and adapted for my own use.

1
  • 2
    On adding new taxonomy using + Add New Category button in meta-box. checkbox will be added instead of radio button. This given piece of code doesn't fulfill the requirements Commented May 27, 2019 at 6:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.