admin管理员组

文章数量:1026989

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?

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?

Share Improve this question asked Mar 26, 2019 at 19:59 Daniel FlippanceDaniel Flippance 1113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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

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

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?

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?

Share Improve this question asked Mar 26, 2019 at 19:59 Daniel FlippanceDaniel Flippance 1113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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

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

本文标签: custom post typesMutiple Select only POSTing 1 value