Why would wp_set_object_terms save only the first value from an array?

I'm using a frontend form to programmatically create a Woocommerce product. The frontend form uses a multiple select field that dynamically pulls in all product categories where each option value is the ID of each product category term. Here's the form:

<form action="" name="form" method="post" id="edit_image_form" enctype="multipart/form-data">
    <div class="form-group">
        <select name="image_category[]" id="image_category" class="form-control select-checkbox" multiple />
                $cat_args = array(
                    'orderby'    => 'name',
                    'order'      => 'asc',
                    'hide_empty' => false,
                    'exclude'    => array(72) //Uncategorised
                $product_categories = get_terms( 'product_cat', $cat_args );
                if( !empty($product_categories) ){
                    foreach ($product_categories as $key => $category) {
                        echo '<option value="'.$category->term_id.'">'.$category->name.'</option>';
    <input type="submit" class="btn btn-primary" value="Add Product" name="submit" />

After selecting 3 terms from the select field, it spits out the following:

array(3) { 
    [0]=> string(3) "143" 
    [1]=> string(3) "145" 
    [2]=> string(3) "113" 

Am I supposed to turn the above array into a comma separated string like below?

$image_category = implode( ", ", $_POST['image_category'] ); //Outputs 143, 145, 113

wp_set_object_terms($product->get_id(), array((int)$image_category), 'product_cat');

If so, it's only saving the first term (143). I can confirm the other three IDs definitely already exist. Where am I going wrong?

1 Answer 1


Turns out someone had the same problem and answered their own problem here.

wp_set_object_terms not accepting variable array

Here's my working solution for my problem:

$image_category = array_map('intval', $_POST['image_category']);
wp_set_object_terms($product->get_id(), $image_category, 'product_cat');

Hope this helps someone else. Thanks for reading.

Your Answer

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

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