0
votes

I have a custom post type, vehicles, I have added two custom meta boxes with the following code (from the codex). The only problem is that when I save they both get the value of the second box. I have searched but cannot find out how to same them as different meta values.

/**
 * Adds a box to the main column on the Post and Page edit screens.
 */
function lm_add_meta_box() {
add_meta_box(
    'lm_vehicles_capacity',
    __( 'Capacity', 'lm_textdomain' ),
    'lm_meta_box_callback1',
    'vehicles',//$screen
    'side',
    'high'
);

add_meta_box(
    'lm_vehicles_upselltext',
    __( 'Upsell Text', 'lm_textdomain' ),
    'lm_meta_box_callback2',
    'vehicles',//$screen
    'side',
    'high'
);
}
add_action( 'add_meta_boxes', 'lm_add_meta_box' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function lm_meta_box_callback1( $post ) {

// Add an nonce field so we can check for it later.
wp_nonce_field( 'lm_meta_box', 'lm_meta_box_nonce' );

/*
 * Use get_post_meta() to retrieve an existing value
 * from the database and use the value for the form.
 */
$value = get_post_meta( $post->ID, '_lm_meta_value_key1', true );

echo '<label for="lm_new_field1">';
_e( 'Description for this field', 'lm_textdomain' );
echo '</label> ';
echo '<input type="text" id="lm_new_field1" name="lm_new_field1" value="' . esc_attr( $value ) . '" size="25" />';
}
function lm_meta_box_callback2( $post ) {

// Add an nonce field so we can check for it later.
wp_nonce_field( 'lm_meta_box', 'lm_meta_box_nonce' );

/*
 * Use get_post_meta() to retrieve an existing value
 * from the database and use the value for the form.
 */
$value = get_post_meta( $post->ID, '_lm_meta_value_key2', true );

echo '<label for="lm_new_field2">';
_e( 'Description for this field', 'lm_textdomain' );
echo '</label> ';
echo '<input type="text" id="lm_new_field2" name="lm_new_field2"     value="' . esc_attr( $value ) . '" size="25" />';
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function lm_save_meta_box_data( $post_id ) {

/*
 * We need to verify this came from our screen and with proper authorization,
 * because the save_post action can be triggered at other times.
 */

// Check if our nonce is set.
if ( ! isset( $_POST['lm_meta_box_nonce'] ) ) {
    return;
}

// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['lm_meta_box_nonce'], 'lm_meta_box' )     ) {
    return;
}

// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return;
}

// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

    if ( ! current_user_can( 'edit_page', $post_id ) ) {
        return;
    }

} else {

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
}

/* OK, it's safe for us to save the data now. */

// Make sure that it is set.
if ( ! isset( $_POST['lm_new_field1'] ) || ! isset( $_POST['lm_new_field2'] ) ) {
    return;
}

// Sanitize user input.
$my_data1 = sanitize_text_field( $_POST['lm_new_field1'] );
$my_data2 = sanitize_text_field( $_POST['lm_new_field2'] );

// Update the meta field in the database.
update_post_meta( $post_id, '_lm_meta_value_key1', $my_data1 );
update_post_meta( $post_id, '_lm_meta_value_key2', $my_data2 );
}
add_action( 'save_post', 'lm_save_meta_box_data' );
2

2 Answers

1
votes

OK there were quite some mistakes in your code. I think you just pulled it from the codex and added a new filed at the top :P. I haven't tested it yet, but all the changes that i think were relevant have been added. There still might be a few minor adjustments to make.

<?php /**
 * Adds a box to the main column on the Post and Page edit screens.
 */
function lm_add_meta_box() {
    add_meta_box(
        'lm_vehicles_capacity',
        __( 'Capacity', 'lm_textdomain' ),
        'lm_meta_box_callback1',
        'vehicles',//$screen
        'side',
        'high'
    );

    add_meta_box(
        'lm_vehicles_upselltext',
        __( 'Upsell Text', 'lm_textdomain' ),
        'lm_meta_box_callback2',
        'vehicles',//$screen
        'side',
        'high'
    );
}
add_action( 'add_meta_boxes', 'lm_add_meta_box' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function lm_meta_box_callback1( $post ) {

    // Add an nonce field so we can check for it later.
    wp_nonce_field( 'lm_meta_box', 'lm_meta_box_nonce' );

    /*
     * Use get_post_meta() to retrieve an existing value
     * from the database and use the value for the form.
     */
    $value = get_post_meta( $post->ID, '_lm_meta_value_key1', true );

    echo '<label for="lm_new_field1">';
    _e( 'Description for this field', 'lm_textdomain' );
    echo '</label> ';
    echo '<input type="text" id="lm_new_field1" name="lm_new_field1" value="' . esc_attr( $value ) . '" size="25" />';
}
function lm_meta_box_callback2( $post ) {

    // Add an nonce field so we can check for it later.
    wp_nonce_field( 'lm_meta_box', 'lm_meta_box_nonce' );

    /*
     * Use get_post_meta() to retrieve an existing value
     * from the database and use the value for the form.
     */
    $value = get_post_meta( $post->ID, '_lm_meta_value_key2', true );

    echo '<label for="lm_new_field2">';
    _e( 'Description for this field', 'lm_textdomain' );
    echo '</label> ';
    echo '<input type="text" id="lm_new_field2" name="lm_new_field2" value="' . esc_attr( $value ) . '" size="25" />';
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function lm_save_meta_box_data( $post_id ) {

    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    // Check if our nonce is set.
    if ( ! isset( $_POST['lm_meta_box_nonce'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['lm_meta_box_nonce'], 'lm_meta_box' ) ) {
        return;
    }

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions.
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }

    } else {

        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }

    /* OK, it's safe for us to save the data now. */

    // Make sure that it is set.
    if ( ! isset( $_POST['lm_new_field1'] ) || ! isset( $_POST['lm_new_field2'] ) ) {
        return;
    }

    // Sanitize user input.
    $my_data1 = sanitize_text_field( $_POST['lm_new_field1'] );
    $my_data2 = sanitize_text_field( $_POST['lm_new_field2'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, '_lm_meta_value_key1', $my_data1 );
    update_post_meta( $post_id, '_lm_meta_value_key2', $my_data2 );
}
add_action( 'save_post', 'lm_save_meta_box_data' );

?>
0
votes

from codex here is what add_meta_box looks like

     add_meta_box( $id, $title, $callback, $screen, $context,
     $priority, $callback_args );

you are using same callback for both metabox and the last one metabox value gets used change callback for both of them .it will work perefect