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?
1 Answer
Reset to default 2Try 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?
1 Answer
Reset to default 2Try adding square brackets after the select element's name:
<select multiple size="6" name="product_ids[]">
本文标签: custom post typesMutiple Select only POSTing 1 value
版权声明:本文标题:custom post types - Mutiple Select only POSTing 1 value 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745656079a2161604.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论