Php 多字段Wordpress元数据库未保存

Php 多字段Wordpress元数据库未保存,php,wordpress,custom-wordpress-pages,meta-boxes,Php,Wordpress,Custom Wordpress Pages,Meta Boxes,我有一个名为“个人团队成员”的自定义页面模板,我需要自定义元数据。“元盒”显示“正确”,并显示“我的字段”。问题是,当我填写任何字段并在帖子上单击“更新”时,它不会在元字段中显示值。我相信这是由我的$metaKey引起的,但我不确定为什么没有保存该值。我已经在下面包含了我所有的metaBox代码,提前谢谢 <?php /* Create one or more meta boxes to be displayed on the post editor screen. */ function

我有一个名为“个人团队成员”的自定义页面模板,我需要自定义元数据。“元盒”显示“正确”,并显示“我的字段”。问题是,当我填写任何字段并在帖子上单击“更新”时,它不会在元字段中显示值。我相信这是由我的$metaKey引起的,但我不确定为什么没有保存该值。我已经在下面包含了我所有的metaBox代码,提前谢谢

<?php
/* Create one or more meta boxes to be displayed on the post editor screen. */
function test_wealth_add_meta_boxes( $post ) {

    // Get the page template post meta
    $page_template = get_post_meta( $post->ID, '_wp_page_template', true );
    // If the current page uses our specific
    // template, then output our custom metabox
    if ( 'individual_team_member.php' == $page_template ) {
        add_meta_box(
            'member-title', // Metabox HTML ID attribute
            'Team Member Details', // Metabox title
            'test_page_template_metabox', // callback name
            'page', // post type
            'side', // context (advanced, normal, or side)
            'high' // priority (high, core, default or low)
        );
    }
}
// Make sure to use "_" instead of "-"
add_action( 'add_meta_boxes_page', 'test_wealth_add_meta_boxes' );


/* Display the post meta box. */
function test_page_template_metabox( $post ) { ?>

    <?php wp_nonce_field( basename( __FILE__ ), 'tidehave_wealth_nonce' ); ?>

    <p>
        <label for="member-title"><?php _e("Title"); ?></label>
        <br />
        <input class="" type="text" name="member-title" id="member-title" value="<?php echo get_post_meta( $post->ID, 'title_meta_field', true ); ?>" size="30" />
    </p>
    <p>
        <label for="member-phone"><?php _e("Phone Number"); ?></label>
        <br />
        <input class="" type="phone" name="member-phone" id="member-phone" value="<?php echo get_post_meta( $post->ID, 'test_page_template_metabox2', true ); ?>" size="30" />
    </p>
    <p>
        <label for="member-email"><?php _e("Email"); ?></label>
        <br />
        <input class="" type="text" name="member-email" id="member-email" value="<?php echo get_post_meta( $post->ID, 'test_page_template_metabox3', true ); ?>" size="30" />
    </p>
<?php }

/* Save the meta box's post metadata. */
function test_wealth_save_meta( $post_id, $post ) {

    /* Verify the nonce before proceeding. */
    if ( !isset( $_POST['tidehave_wealth_nonce'] ) || !wp_verify_nonce( $_POST['tidehave_wealth_nonce'], basename( __FILE__ ) ) )
        return $post_id;

    /* Get the post type object. */
    $post_type = get_post_type_object( $post->post_type );

    /* Check if the current user has permission to edit the post. */
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;

    /* Get the posted data. */
    $title_meta_value = isset( $_POST['member-title'] );
    $phone_meta_value = isset( $_POST['member-phone'] );
    $email_meta_value = isset( $_POST['member-email'] );

    /* Get the meta key. */
    $meta_key1 = 'title_meta_field';
    $meta_key2 = 'test_page_template_metabox2';
    $meta_key3 = 'test_page_template_metabox3';

    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key1, true );
    $meta_value = get_post_meta( $post_id, $meta_key2, true );
    $meta_value = get_post_meta( $post_id, $meta_key3, true );

    /* If a new meta value was added and there was no previous value, add it. */
    if ( $title_meta_value && '' == $meta_value )
        add_post_meta( $post_id, $meta_key1, $title_meta_value, true );

    /* If the new meta value does not match the old value, update it. */
    elseif ( $title_meta_value && $title_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key1, $title_meta_value );

    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( '' == $title_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key1, $meta_value );

    /* If a new meta value was added and there was no previous value, add it. */
    // if ( $phone_meta_value && '' == $meta_value )
    //  add_post_meta( $post_id, $meta_key2, $phone_meta_value, true );

    // /* If the new meta value does not match the old value, update it. */
    // elseif ( $phone_meta_value && $new_meta_value != $meta_value )
    //  update_post_meta( $post_id, $meta_key2, $phone_meta_value );

    // /* If there is no new meta value but an old value exists, delete it. */
    // elseif ( '' == $phone_meta_value && $meta_value )
    //  delete_post_meta( $post_id, $meta_key2, $meta_value );

    // /* If a new meta value was added and there was no previous value, add it. */
    // if ( $email_meta_value && '' == $meta_value )
    //  add_post_meta( $post_id, $meta_key3, $email_meta_value, true );

    // /* If the new meta value does not match the old value, update it. */
    // elseif ( $new_meta_value && $new_meta_value != $meta_value )
    //  update_post_meta( $post_id, $meta_key3, $email_meta_value );

    // /* If there is no new meta value but an old value exists, delete it. */
    // elseif ( '' == $email_meta_value && $meta_value )
    //  delete_post_meta( $post_id, $meta_key3, $meta_value );
}

/* Save post meta on the 'save_post' hook. */
add_action( 'save_post', 'test_wealth_save_meta', 10, 3 );
替换此项:

    /* Get the posted data. */
    $title_meta_value = isset( $_POST['member-title'] );
    $phone_meta_value = isset( $_POST['member-phone'] );
    $email_meta_value = isset( $_POST['member-email'] );
。。为此:

    /* Get the posted data. */
    $title_meta_value = isset( $_POST['member-title'] ) ? $_POST['member-title'] : '';
    $phone_meta_value = isset( $_POST['member-phone'] ) ? $_POST['member-phone'] : '';
    $email_meta_value = isset( $_POST['member-email'] ) ? $_POST['member-email'] : '';
或者你可以缩短这个时间:

    /* Get the posted data. */
    $title_meta_value = isset( $_POST['member-title'] );
    $phone_meta_value = isset( $_POST['member-phone'] );
    $email_meta_value = isset( $_POST['member-email'] );

    /* Get the meta key. */
    $meta_key1 = 'title_meta_field';
    $meta_key2 = 'test_page_template_metabox2';
    $meta_key3 = 'test_page_template_metabox3';

    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key1, true );
    $meta_value = get_post_meta( $post_id, $meta_key2, true );
    $meta_value = get_post_meta( $post_id, $meta_key3, true );

    /* If a new meta value was added and there was no previous value, add it. */
    if ( $title_meta_value && '' == $meta_value )
        add_post_meta( $post_id, $meta_key1, $title_meta_value, true );

    /* If the new meta value does not match the old value, update it. */
    elseif ( $title_meta_value && $title_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key1, $title_meta_value );

    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( '' == $title_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key1, $meta_value );

    /* If a new meta value was added and there was no previous value, add it. */
    // if ( $phone_meta_value && '' == $meta_value )
    //  add_post_meta( $post_id, $meta_key2, $phone_meta_value, true );

    // /* If the new meta value does not match the old value, update it. */
    // elseif ( $phone_meta_value && $new_meta_value != $meta_value )
    //  update_post_meta( $post_id, $meta_key2, $phone_meta_value );

    // /* If there is no new meta value but an old value exists, delete it. */
    // elseif ( '' == $phone_meta_value && $meta_value )
    //  delete_post_meta( $post_id, $meta_key2, $meta_value );

    // /* If a new meta value was added and there was no previous value, add it. */
    // if ( $email_meta_value && '' == $meta_value )
    //  add_post_meta( $post_id, $meta_key3, $email_meta_value, true );

    // /* If the new meta value does not match the old value, update it. */
    // elseif ( $new_meta_value && $new_meta_value != $meta_value )
    //  update_post_meta( $post_id, $meta_key3, $email_meta_value );

    // /* If there is no new meta value but an old value exists, delete it. */
    // elseif ( '' == $email_meta_value && $meta_value )
    //  delete_post_meta( $post_id, $meta_key3, $meta_value );
。。为此:

foreach ( array(
    'member-title' => 'title_meta_field',
    'member-phone' => 'test_page_template_metabox2',
    'member-email' => 'test_page_template_metabox3',
) as $input_name => $meta_key ) {
    /* Get the posted data. */
    $input_value = isset( $_POST[ $input_name ] ) ? $_POST[ $input_name ] : '';

    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key, true );

    /* If a new meta value was added and there was no previous value, add it. */
    if ( $input_value && '' == $meta_value )
        add_post_meta( $post_id, $meta_key, $input_value, true );

    /* If the new meta value does not match the old value, update it. */
    elseif ( $input_value && $input_value != $meta_value )
        update_post_meta( $post_id, $meta_key, $input_value );

    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( '' == $input_value && $meta_value )
        delete_post_meta( $post_id, $meta_key, $meta_value );
}