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' ] ) );
}