Still not sure what exactly what you aiming for, but having come back to this question i wanted to at least try to offer a solution.
So try this out, and see if that’s the kind of thing you had in mind with the dropdown… ?
add_action( 'add_meta_boxes', 'add_inventory_metaboxes' );
function add_inventory_metaboxes() {
add_meta_box('inventory_information', 'Inventory Information', 'inventory_information', 'inventory', 'side', 'default');
}
function inventory_dropdown_opts() {
return array(
'Option 1' => 1,
'Option 2' => 2,
'Option 3' => 3,
'Option 4' => 4,
);
}
function inventory_information() {
global $post;
$stk_num = get_post_meta( $post->ID, '_dappcf_i_stocknum', true );
$vin_num = get_post_meta( $post->ID, '_dappcf_i_vin', true );
$drp_dwn = get_post_meta( $post->ID, '_dappcf_i_dropdown', true );
if( empty( $drp_dwn ) )
$drp_dwn = 0;
wp_nonce_field( 'inventory_nonce', 'inventorymeta_noncename' );
?>
<p>
<label>Stock #</label>
<input type="text" name="_dappcf_i_stocknum" value="<?php echo $stk_num; ?>" class="small-text" />
</p>
<p>
<label>VIN</label>
<input type="text" name="_dappcf_i_vin" value="<?php echo $vin_num; ?>" class="widefat" /></p>
<p>
<label>Inventory class</label>
<select name="_dappcf_i_dropdown" class="widefat">
<?php foreach( inventory_dropdown_opts() as $text => $value ) : ?>
<option value="<?php echo $value; ?>"<?php selected( $value == $drp_dwn ); ?>><?php echo $text; ?></option>
<?php endforeach; ?>
</select>
</p>
<?php
}
add_action('save_post', 'txpbs_save_events_meta', 1, 2);
function txpbs_save_events_meta( $post_id, $post ) {
if(
!wp_verify_nonce( $_POST['inventorymeta_noncename'], 'inventory_nonce' ) ||
!current_user_can( 'edit_post', $post_id ) ||
$post->post_type == 'revision'
)
return;
foreach( array( '_dappcf_i_stocknum', '_dappcf_i_vin', '_dappcf_i_dropdown' ) as $field ) {
if( isset( $_POST[$field] ) ) {
if( '_dappcf_i_dropdown' == $field ) {
if( in_array( $_POST[$field], inventory_dropdown_opts() ) )
update_post_meta( $post_id, $field, $_POST[$field] );
continue;
}
update_post_meta( $post_id, $field, $_POST[$field] );
continue;
}
delete_post_meta( $post_id, $key );
}
}
Follow-up #1
If you need to show the dropdown in the front end, just do it the same way i did inside the inventory_information
function, like so..
<select name="add-a-name-here" class="widefat">
<?php foreach( inventory_dropdown_opts() as $text => $value ) : ?>
<option value="<?php echo $value; ?>"><?php echo $text; ?></option>
<?php endforeach; ?>
</select>
Your dropdown options are defined in the inventory_dropdown_opts
function, so you can use the same both front and admin side, and see the same data.