I'm building my own metabox with checkboxes. I want to save the values of the checkboxes I've cheked as categories (IDs).

I have build an array to append the values of the checkboxes, but it only saves the last checkbox I have checked.

I'm trying to save the values using the following functions.

wp_set_post_categories(); wp_set_object_terms();

In addition, when I click the submit button for create / edit post, whatever the function I use, it always saves the last value of checkbox.

Here is my code:

      add_action( 'admin_menu', 'my_enhanced_categories_remove_meta_box');
function my_enhanced_categories_remove_meta_box(){
   remove_meta_box('categorydiv', 'post', 'normal');

//Add new taxonomy meta box
 add_action( 'add_meta_boxes', 'my_enhanced_categories_add_meta_box');
 function my_enhanced_categories_add_meta_box() {
     add_meta_box( 'category-all', 'Custom Portfolio Categories','my_enhanced_categories_category_metabox','post' ,'side','high');

$postterms = get_the_terms( $post->ID,$taxonomy );

  function my_enhanced_categories_category_metabox( $post ) {

      //Set up the taxonomy object and get terms
$taxonomy = 'category';
$tax = get_taxonomy($taxonomy);//This is the taxonomy object

//The name of the form
$name = 'tax_input[' . $taxonomy . ']';

//Get all the terms for this taxonomy
$terms = get_terms($taxonomy,array('hide_empty' => 0));
     echo '<div id="' . $taxonomy . '-all" class="tabs-panel">';
     echo '<ul id="' . $taxonomy . '-checklist" class="list-' . $taxonomy . 'categorychecklist form-no-clear">';

     $categories = array();

     foreach($terms as $term){
            $id = $taxonomy.'-'.$term->term_id;

             echo "<li id='$id'><label>";

            echo "<input type='checkbox' id='in-$id' name='{$name}-$term->term_id' value='$term->term_id' />$term->name<br />";

            echo "</label></li>";

$key_tax = ($_POST['tax_input']);

if (!empty($key_tax)) {

array_push($categories, array_values($key_tax));

    /*wp_set_post_categories( $_POST['post_ID'], array_values($key_tax)[0]); */





//    var_dump($categories);
  //          wp_set_post_terms( $_POST['post_ID'], $categories, 'category' );

wp_set_object_terms( $_POST['post_ID'], $categories, 'category');

1 Answer 1


This is fairly simple to achieve, first rename the name attribute to my_categories[] this is then passed as an array.

Then you will need set the values of each checkbox as the category id. In this way you can loop though the array my_categories[] and then set the categories present in that array.

Here is a simplified example :

// In your callback function for metabox,
// This should be inside a loop.
<input type="checkbox" name="my_categories[]" value="$term->term_id">

// This should be hooked to the save_post action of appropriate post type.
if(!empty($_POST['my_categories'])) { 
    foreach($_POST['my_categories'] as $cat) 
            echo $cat; 
            //echoes the value set in the HTML form for each checked checkbox.
            //In your case the ids of selected checkboxes, now all you need to do is set those categories.

Now you have all the ids of selected categories in an array, which is easier to process. Use your logic to set the categories.

  • Thanks @kabiir, the example solved my main question. Now, I'm trying to apply it to a custom content type, in my case "portfolio". This "portfolio" content type has "portfolio_categories", but when I remove his metabox and add my own metabox, doesn't apply the changes.
    – Tiyome
    Feb 15, 2017 at 8:55
  • This is a different problem, ask another question. Feb 15, 2017 at 8:56
  • Then post the link to that question in this comment and I'll look into it. Feb 15, 2017 at 8:57
  • Thanks @kabiir wordpress.stackexchange.com/questions/256534/…
    – Tiyome
    Feb 15, 2017 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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