WP Metaboxes Multi选择更新,获取当前信息

问题描述 投票:0回答:1

当前在我的插件中不起作用的最后一件事是多选输入字段。如图所示,可以选择项目,但是更新帖子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=""><?php _e( 'Please select', 'nutriplus' ) ?></option>-->
        <option value="build-body" <?php selected($current_diet_goals, 'build-body'); ?>><?php _e( 'Build Body', 'nutriplus' ) ?></option>
        <option value="build-muscle" <?php selected($current_diet_goals, 'build-muscle'); ?>><?php _e( 'Build Muscle', 'nutriplus' ) ?></option>
        <option value="eat-healthy" <?php selected($current_diet_goals, 'eat-healthy'); ?>><?php _e( 'Eat Healthy', 'nutriplus' ) ?></option>
        <option value="gain-weight" <?php selected($current_diet_goals, 'gain-weight'); ?>><?php _e( 'Gain Weight', 'nutriplus' ) ?></option>
        <option value="hold-weight" <?php selected($current_diet_goals, 'hold-weight'); ?>><?php _e( 'Hold Weight', 'nutriplus' ) ?></option>
        <option value="improve-condition" <?php selected($current_diet_goals, 'improve-condition'); ?>><?php _e( 'Improve Condition', 'nutriplus' ) ?></option>
        <option value="improve-performance" <?php selected($current_diet_goals, 'improve-performance'); ?>><?php _e( 'Improve Performance', 'nutriplus' ) ?></option>
        <option value="lose-fat" <?php selected($current_diet_goals, 'lose-fat'); ?>><?php _e( 'Lose Fat', 'nutriplus' ) ?></option>
        <option value="lose-weight" <?php selected($current_diet_goals, 'lose-weight'); ?>><?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函数,我也认为字段名称。我在互联网上进行了大量搜索以解决此问题,但未能成功反映出在我的代码中找到的解决方案:(

谢谢

wordpress post metadata
1个回答
0
投票

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

使用diet_goals[]代替diet_goals

并在以下位置更改您选择的功能:

selected( in_array( 'some-value', $current_diet_goals, true ) );
© www.soinside.com 2019 - 2024. All rights reserved.