1

I have a select dropdown in my custom meta box in the admin area. It offers two options Approved and In Progress. When I select an option and save it the database is updated, but the value in the admin area is not. What I see is the first item from the list displayed in the select box. i.e. if I select In Progress and save the admin shows Approved as the selected value even though it is saved as In Progress in the database and displays In Progress on the front end of the site.

Here is my code:

<label for="myplugin_meta_box_select">Status:</label>
<select name="myplugin_meta_box_select" id="myplugin_meta_box_select">
  <option value="Approved" <?php selected( $selected, 'approved' ); ?>>Approved</option>
  <option value="In Progress" <?php selected( $selected, 'inprogress' ); ?>>In Progress</option>
</select>

And I save it with this:

if( isset( $_POST['myplugin_meta_box_select'] ) )
        update_post_meta( $post_id, 'myplugin_meta_box_select', esc_attr( $_POST['myplugin_meta_box_select'] ) );

What have I missed out?

2

The second parameter of selected() has to agree with the value attribute of the current <option>. So assuming you have stuffed $selected with the value given by the relevant get_post_meta(), the following should work for you:

<label for="myplugin_meta_box_select">Status:</label>
<select name="myplugin_meta_box_select" id="myplugin_meta_box_select">
  <option value="Approved" <?php selected( $selected, 'Approved' ); ?>>Approved</option>
  <option value="In Progress" <?php selected( $selected, 'In Progress' ); ?>>In Progress</option>
</select>

Side note: I'd personally prefer to use the "sluggish" version (i.e. inprogress instead of In Progress) for the value attribute (and thus for the post meta value), but you'd have to make changes to your front-end as well.

  • Thanks. This doesn't change anything for me. The database is still updating but the backend still won't show the current value and is goig back to the default. – MrFox Mar 26 '15 at 12:06
  • Make sure that $selected contains the appropriate value as per my assumption above (do a print_r(), var_dump(), error_log() or whatever suits you best to cross-check). I don't see any other possible issues. – geomagas Mar 26 '15 at 12:45
  • Thanks, but I don't know what's going on with my custom meta box. The only way I can get this to work is by adding a totally new meta box that contains no tick boxes or text fields and use that for my status drop down rather than adding it to my exisiting meta box. Everything works, but not the way I had meant it to. – MrFox Mar 26 '15 at 14:51
  • Then I'm pretty sure the problem is caused by another part of your code. Maybe you should have posted the whole thing in the first place. – geomagas Mar 26 '15 at 17:17
  • Well, that didn't work either. You must be right, I must have messed something up when setting up my code in the first place. – MrFox Mar 27 '15 at 9:20
2

This is what worked for me in the end. I used this to add a metabox:

<?php add_action( 'add_meta_boxes', 'so_custom_meta_box' );
function so_custom_meta_box($post){
add_meta_box('so_meta_box', 'Additional', 'custom_element_grid_class_meta_box',     'event', 'normal' , 'high');
}
add_action('save_post', 'so_save_metabox');

function so_save_metabox(){ 
global $post;
if(isset($_POST["custom_element_grid_class"])){
    $region = $_POST['region_sel'];
    update_post_meta($post->ID, 'region_sel_meta_box', $region);
}
}
function custom_element_grid_class_meta_box($post){
$region = get_post_meta($post->ID, 'region_sel_meta_box', true);
?>   
<label>Region:  </label>
<select name="region_sel" id="region_sel"> 
    <option value="Region1" <?php selected( $region, 'Region1' ); ?>>Region1</option>
    <option value="Region2" <?php selected( $region, 'Region2' ); ?>>Region2</option>
</select>
<?php } ?>

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.