My function below, will take the values from my custom meta fields (after a post has been edited, and save or publish has been clicked) and update or insert the posted meta values.
However, if the user leaves this field blank, I believe I want to delete the meta altogether (so I can test for its presence and display accordingly vs just checking for "").
For example, one of my meta options gives the user the ability to add a Custom title to their post, which when present, will populate the page's tag. However, if the field is left empty, I want to default the tag to the_title(), which is simply the Post title used to identify the page/post.
Since I'm not deleting the meta on save, its always present after the first time a user enters something in there, get_post_meta($post-ID,'MyCustomTitle', true) is always true. Further, they cannot blank it out by clearing the title field and hitting publish.
What am I missing in the save in order to clear the value to "" when the user clears the field?
if ($_POST['MyCustomTitle']) {
update_custom_meta($postID, $_POST['MyCustomTitle'], 'MyCustomTitle');
}
function update_custom_meta($postID, $newvalue, $field_name) {
// To create new meta
if(!get_post_meta($postID, $field_name)){
add_post_meta($postID, $field_name, $newvalue);
}else{
// or to update existing meta
update_post_meta($postID, $field_name, $newvalue);
}
}