0

In a custom post type edit form, I have a select marked as multiple to allow multiple items to be selected:

<select multiple size="6" name="product_ids">
    <option value="71">First</option>
    <option value="65">Second</option>
    ...
</select>

Then in PHP I have a save action to save the result:

class PromotionPostType {
    public function __construct() {
        ...
        add_action('save_post_promotion', array($this, 'savePromotion'));
    }

    //========================================================
    // savePromotion
    //========================================================
    public function savePromotion() {
        global $post;
        ...
        if (!is_null($post) && 'promotion' == get_post_type()) {
            error_log('DEBUG REQUEST: '.json_encode($_REQUEST));
            error_log('DEBUG POST: '.json_encode($_POST));
        }
    }    
}

I run the page, select 2 items in the multiple select list and save the post.

Looking at the debug output of the save method, I see that the product_ids is a string with just one value rather than an array (or serialized array) with 2 values.

{
    "ID": 78,
    ...
    "post_ID": "78",
    "post_type": "promotion",
    ...
    "product_ids": "65",
}

Why is the product_ids value not returning both of the selected IDs?

2

Try adding square brackets after the select element's name:

<select multiple size="6" name="product_ids[]"> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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