WP Metaboxes多选更新,获取最新信息



我的插件中目前不起作用的最后一件事是多选输入字段。如图所示,可以选择项目,但更新帖子1(不会保存选定的值,2(进入编辑屏幕时不会加载这些值。

我的代码:

<?php
// METABOX I Diet Information
function diet_add_meta_box1() {
add_meta_box( 'diet_meta_box1', __( 'Diet Information', 'nutriplus' ), 'diet_build_meta_box1', "np-diet", "side", "high", null);
}
add_action( 'add_meta_boxes', 'diet_add_meta_box1' );
/**
* Build custom field METABOX I
*
* @param post $post The post object
*/
function diet_build_meta_box1( $post ){
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'diet_meta_box1_nonce' );
// retrieve the current values
$current_diet_goals = get_post_meta( $post->ID, '_diet_diet_goals', false );
?>
<div>
<label for="diet-goals" class="np-label"><strong><?php _e( 'Diet Goals', 'nutriplus' ) ?></strong></label>
<p>
<select name="diet_goals[]" id="np-dropdown" class="np-dropdown" multiple>
<option value="build-body" <?php selected( in_array( 'build-body', $current_diet_goals, true ) ); ?>><?php _e( 'Build Body', 'nutriplus' ) ?></option>
<option value="build-muscle" <?php selected( in_array( 'build-muscle', $current_diet_goals, true ) ); ?>><?php _e( 'Build Muscle', 'nutriplus' ) ?></option>
<option value="eat-healthy" <?php selected( in_array( 'eat-healthy', $current_diet_goals, true ) ); ?>><?php _e( 'Eat Healthy', 'nutriplus' ) ?></option>
<option value="gain-weight" <?php selected( in_array( 'gain-weight', $current_diet_goals, true ) ); ?>><?php _e( 'Gain Weight', 'nutriplus' ) ?></option>
<option value="hold-weight" <?php selected( in_array( 'hold-weight', $current_diet_goals, true ) ); ?>><?php _e( 'Hold Weight', 'nutriplus' ) ?></option>
<option value="improve-condition" <?php selected( in_array( 'improve-condition', $current_diet_goals, true ) ); ?>><?php _e( 'Improve Condition', 'nutriplus' ) ?></option>
<option value="improve-performance" <?php selected( in_array( 'improve-performance', $current_diet_goals, true ) ); ?>><?php _e( 'Improve Performance', 'nutriplus' ) ?></option>
<option value="lose-fat" <?php selected( in_array( 'lose-fat', $current_diet_goals, true ) ); ?>><?php _e( 'Lose Fat', 'nutriplus' ) ?></option>
<option value="lose-weight" <?php selected( in_array( 'lose-weight', $current_diet_goals, true ) ); ?>><?php _e( 'Lose Weight', 'nutriplus' ) ?></option>
</select>
</p>
</div>
<?php
}
function diet_save_meta_box1_data( $post_id ){
// verify meta box nonce
if ( !isset( $_POST['diet_meta_box1_nonce'] ) || !wp_verify_nonce( $_POST['diet_meta_box1_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}   
// store custom fields values METABOX1
if ( isset( $_REQUEST['diet_goals'] ) ) {
$array = array_map( 'sanitize_text_field', wp_unslash( $_POST['diet_goals'] ) );
update_post_meta( $post_id, '_diet_diet_goals',  $array );
}
}
add_action( 'save_post', 'diet_save_meta_box1_data' );
?>

我知道可能出了什么问题。也许这部分:

$current_diet_goals = get_post_meta( $post->ID, '_diet_diet_goals', false );

显然,save_meta函数和字段名也是如此。我在互联网上搜索了很多来解决这个问题,但我没有成功地镜像我在代码上找到的解决方案:(

谢谢

您需要在select中更改属性名称。

使用diet_goals[]代替diet_goals

并更改您在上选择的功能

selected( in_array( 'some-value', $current_diet_goals, true ) );