0
<p>
    <?php
        $options = get_option( 'city' );
        $names = explode( PHP_EOL, $options );
        ?>
        <label for="City" class="timeshare-row-title"><?php _e( 'City:', 'timeshare-textdomain' )?></label>
        <select name="city" id="city">
        <?php foreach ( $names as $name ) {
            printf(
                '<option value="%s" selected="selected">%s</option>',
                $name,
                $name
            );
        } ?>

        </select>
</p>

Save function:

 if( isset( $_POST[ 'city' ] ) ) {
    update_post_meta( $post_id, 'city', sanitize_text_field( $_POST[ 'city' ] ) );
}

This is code used for a working select metabox, this one is done with hard coded optios.

<p>
    <label for="beds" class="timeshare-row-title"><?php _e( 'Bedrooms:', 'timeshare-textdomain' )?></label>
    <select name="beds" id="beds">
        <option value="1" <?php if ( isset ( $timeshare_stored_meta['beds'] ) ) selected( $timeshare_stored_meta['beds'][0], '1' ); ?>><?php _e( '1', 'timeshare-textdomain' )?></option>';
        <option value="2" <?php if ( isset ( $timeshare_stored_meta['beds'] ) ) selected( $timeshare_stored_meta['beds'][0], '2' ); ?>><?php _e( '2', 'timeshare-textdomain' )?></option>';
        <option value="3" <?php if ( isset ( $timeshare_stored_meta['beds'] ) ) selected( $timeshare_stored_meta['beds'][0], '3' ); ?>><?php _e( '3', 'timeshare-textdomain' )?></option>';
        <option value="4" <?php if ( isset ( $timeshare_stored_meta['beds'] ) ) selected( $timeshare_stored_meta['beds'][0], '4' ); ?>><?php _e( '4', 'timeshare-textdomain' )?></option>';
    </select>
</p>

and the save code for it:
     // Checks for input and saves if needed
    if( isset( $_POST[ 'beds' ] ) ) {
    update_post_meta( $post_id, 'beds', $_POST[ 'beds' ] );
}

Save function:

function timeshare_meta_save( $post_id ) {

// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'timeshare_nonce' ] ) && wp_verify_nonce( $_POST[ 'timeshare_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
    return;
}
1

From your posted Github files, the function that prints the meta box is this

function timeshare_meta_callback( $post ) {

As you can see, $post is passed to this function. It's a WP_Post Object which contains all the information of that post.

WP_Post has a member variable named ID, which is needed to get the current post meta value that is saved. So, you need to do this get the post id

$post_id = $post->ID;

Here's the complete code

<p>
    <?php
        $post_id = $post->ID;
        $options = get_option( 'city' );
        $city_value = get_post_meta( $post_id, 'city', true );
        $names = explode( PHP_EOL, $options );
        ?>
        <label for="City" class="timeshare-row-title"><?php _e( 'City:', 'timeshare-textdomain' )?></label>
        <select name="city" id="city">
        <?php foreach ( $names as $name ) {
            printf(
                '<option value="%s" %s>%s</option>',
                $name,
                selected($name, $city_value, false),
                $name
            );
        } ?>

        </select>
</p>

Contrarily during saving only post id is passed, which can be seen from here.

function timeshare_meta_save( $post_id ) {

So, you could do this to save the post meta value directly

    if( isset( $_POST[ 'city' ] ) ) {
        update_post_meta( $post_id, 'city', sanitize_text_field( $_POST[ 'city' ] ) );
    }
  • @sakibmoon-still a no go, it defaults to the first item in the created list. – John Lucey Nov 16 '15 at 16:07
  • @JohnLucey I thought that you are saving the option as 'city', but as it turns out you are saving the whole city in that option field. Please post the code of how are you saving them in the db. – sakibmoon Nov 16 '15 at 17:36
  • In the Database I can see the field value changes when I change the choice. The DB is being updated currently. Its the metabox that is not. I do not know if this matters but the select box values are being generated from a textarea in my Plugin Settings page. Hence the get_options usage. – John Lucey Nov 16 '15 at 17:37
  • @sakibmoon- I updated the code to show the corresponding save code. – John Lucey Nov 16 '15 at 17:42
  • @JohnLucey I've updated the code. – sakibmoon Nov 17 '15 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.