Displaying Saved Meta Box Data in Drop Down with selected()

<?php selected() ?> compares the value of the stored data and which option should appear first, my error was that I wasn’t storing the variable to compare it to correctly.

$selected = isset( $values['location'] ) ? esc_attr( $values['location'][0] ) : ”;

should be changed to:

$selected = isset( $custom['location'] ) ? esc_attr( $custom['location'][0] ) : ”;

It was a simple syntax error on my part, as I was referring to the variable incorrectly. Once that was changed, the function <?php selected( $selected, 'Option 1' ); ?> will compare the variable vs. the string in place and then display selected="selected" to place that drop-down item on top.

Leave a Comment