You have add_post_meta()
and update_post_meta()
, but you only need the latter:
This may be used in place of
add_post_meta()
function. The first thing
this function will do is make sure that$meta_key
already exists on
$post_id
. If it does not,add_post_meta($post_id, $meta_key,
is called instead and its result is returned.
$meta_value)
What you are doing now with both functions back to back is pointless and is causing double writes to the database.
Second, assuming that the rest of your code works, update_post_meta()
itself should overwrite any previous data for the post ID and key in question. You don’t need to explicitly delete the value. The only time you’d need more complicated logic would be if you wanted to delete data but not replace it, and that doesn’t appear to be what you are trying to do (though I could be wrong). I’d add an explicit “delete data” checkbox to the form for that case, then use delete_post_meta()
.