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?
1 Answer
Reset to default 2Try adding square brackets after the select element's name:
<select multiple size="6" name="product_ids[]">
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745656081a4638550.html
评论列表(0条)