So im trying to save a dropdown value in my custom post type. What am I doing wrong?
Markup:
<li><label>Location</label><input name="tf_events_location" value="<?php echo $meta_loc; ?>" /></li>
<li><label>Band relation</label>
<select name="tf_events_relate" id="tf_events_relate">
<option value="">All Artists </option>
<option value="146">Mormor</option>
<option value="140">John Face</option>
</select>
</li>
And save
function save_tf_events(){
global $post;
if(!isset($_POST["tf_events_location"])):
return $post;
endif;
$updateloc = $_POST["tf_events_location"];
update_post_meta($post->ID, "tf_events_location", $updateloc);
if(!isset($_POST["tf_events_relate"])):
return $post;
endif;
$updaterelate = $_POST["tf_events_relate"];
update_post_meta($post->ID, "tf_events_relate", $updaterelate);
}
My tf_events_location gets saved allright, but my tf_events_relate is not doing anything.
I think i might be getting the principle wrong 😉
The issue is that you are simply not telling the
<select>element what<option>is selected when the page comes back.There are a number of different ways to do that, some more flexible than others, but in it’s most simple format with the html code you have above, this would work:
It isn’t the most elegant, but it should work.