Saving multiple Metabox contents

Aha! Stupid error. I didn’t give it a proper METAKEY!

This seems to have fixed things:

/* Save the meta box's post metadata. */
function save_metabox_exhibition( $post_id, $post ) {
$fields = 0;

if($_POST) {
    $metaboxes[0]['nonce'] = $_POST['exhibition_finish_date_nonce'];
    $metaboxes[0]['data'] = $_POST['gc_finish_date'];
    $metaboxes[0]['meta'] = 'gc_finish_date';
    $metaboxes[1]['nonce'] = $_POST['exhibition_start_date_nonce'];
    $metaboxes[1]['data'] = $_POST['gc_start_date'];
    $metaboxes[1]['meta'] = 'gc_start_date';
    $metaboxes[2]['nonce'] = $_POST['exhibition_subtitle_nonce'];
    $metaboxes[2]['data'] = $_POST['gc_subtitle'];
    $metaboxes[2]['meta'] = 'gc_subtitle';
    $fields = count($metaboxes);
}


for($i = 0; $i <= $fields ; $i++ ) {
    /* Verify the nonce before proceeding. */
    if ( !isset( $metaboxes[$i]['nonce'] ) || !wp_verify_nonce( $metaboxes[$i]['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 and sanitize it */
    $new_meta_value = ( isset( $metaboxes[$i]['data'] ) ? sanitize_text_field(    $metaboxes[$i]['data'] ) : '' );

    /* Get the meta key. */
    $meta_key = $metaboxes[$i]['meta'];

    /* 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 ( $new_meta_value && '' == $meta_value )
            add_post_meta( $post_id, $meta_key, $new_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_key, $new_meta_value );

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

Leave a Comment