You don’t need to check if meta data exists prior to adding/updating it. update_post_meta
does this for you. (See Codex)
update_post_meta($post_id, $meta_key, $meta_value, $prev_value);
The fourth (optional) $prev_value
argument, if set, will update only $meta_key-$prev_value
pairs. If not set, it will update all values for that $meta_key
for that post. If it can’t find that $meta_key
, it will create a new record.
The add_post_meta
, by default, will add a record regardless if the key already exists. It seems this is what is happening, so try var_dump
-ing get_post_meta($post->ID, $key, FALSE)
and die-ing to check what it is returning.
EDIT
I’ve not checked this, but since your post meta does not have an underscore, it may appear twice on the edit post page (once in your custom metabox, and once in the custom fields section (this may be hidden, check the screen options). This could be causing both the new meta value the old values to be saved to the database…