I have been working on a plugin that uses text area metaboxes on an options page to populate drop downs in the CPT area. I was with the help of some others able to get the list to show in the select boxes and was able to save the choice to the DB. So far I have been unsuccessful at showing the selected item in the CPT area. Is there an easier way of accomplishing a user editable select box? Or can someone explain why selected is not functioning in this code? I have used var_dump() to verify the variables return the expected outcome. It is like the selected statement is ignored. See code below:
<?php
$post_id = $post->ID;
$options = get_option( 'name' );
$names = explode( PHP_EOL, $options );
$name_value = get_post_meta( $post_id, 'name', true );
echo '<pre>'; var_dump($name_value); echo '</pre>';
?>
<label for="name" class="timeshare-row-title"><?php _e( 'Property Name:', 'timeshare-textdomain' )?></label>
<select name="name" id="name">
<?php foreach ( $names as $name ) {
printf(
'<option value="%s" %s>%s</option>',
$name,
selected($name, trim( $name_value ), false),
$name
);
} ?>
</select>
name_value
andname
are equal with same capital and small letter etc