3

This question already has an answer here:

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

marked as duplicate by Howdy_McGee, Pieter Goosen, TheDeadMedic, Mayeenul Islam, fuxia May 17 '15 at 13:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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. – helgatheviking May 7 '15 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 May 13 '15 at 23:50
  • @gdaniel could you share how are you doing this? – June May 15 '15 at 6:54
7

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.

  • 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 – Asad Manzoor May 27 at 6:15

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