Title: update_post_meta problem saving field values
Last modified: August 20, 2016

---

# update_post_meta problem saving field values

 *  [r0bbiem](https://wordpress.org/support/users/r0bbiem/)
 * (@r0bbiem)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/update_post_meta-problem-saving-field-values/)
 * I have a bunch of custom meta boxes and fields associated with a post type, all
   created in a plugin file. I can’t seem to get the values to save or persist when
   I hit the update button.
 * Here’s my code [http://pastebin.com/mw389heA](http://pastebin.com/mw389heA)
 * Hope someone can get me out of the woods with this one, many thanks in advance:]

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/update_post_meta-problem-saving-field-values/#post-3513865)
 * You may be getting an ID collision with the last post in the last loop run. You
   do not need the `global $post;` line in the function initYoutubeMb() because 
   $post (the one being edited) is passed as a parameter. The global $post is the
   last post in the last loop, not the one being edited.
 * I’m not confident this is the problem, but it’s something to address anyway.
 *  Thread Starter [r0bbiem](https://wordpress.org/support/users/r0bbiem/)
 * (@r0bbiem)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/update_post_meta-problem-saving-field-values/#post-3513871)
 * Thanks bcworkz, that didn’t solve the problem but good to know.
 * I have a nagging feeling that I’m not defining my $meta_keys (such as ‘track_artist’)
   before I try to populate them, but I’m not familiar enough with this process.
 * Should I be using an add_post_meta() before I use update_post_meta()?
    Only problem
   I can see with this is that add_post_meta() requires a value and I would just
   want to define the keys at first without populating them with values.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/update_post_meta-problem-saving-field-values/#post-3513900)
 * It should not matter, most WP update functions will add if the key does not exist,
   a quick read of the source of the underlying update_metadata() indicates the 
   same situation here. Since we’re just scratching our heads here, wouldn’t hurt
   to try using add_post_meta(). You could always insert a temporary “tbd” value
   or something. I doubt it’ll make any difference though.
 * I really don’t see any glaring problems with your code, it’s pretty straight 
   forward. All I can suggest at this point is to insert `var_dump($testvalue); 
   die();` at various points to verify actions are firing and functions are returning
   expected values. Good luck!
 *  [MickR](https://wordpress.org/support/users/mickr/)
 * (@mickr)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/update_post_meta-problem-saving-field-values/#post-3513988)
 * Hi rObbiem,
 * you might have got past this by now, but had a similar prob when trying out something
   similar here (update_post_meta not creating a new entry).
 * [http://codex.wordpress.org/Function_Reference/add_post_meta](http://codex.wordpress.org/Function_Reference/add_post_meta)
   provided an answer – basically use add and update in the same instruction and
   if the key doesn’t exist it will create one, eg:
 * <?php add_post_meta(id, ‘key’, $data, true) or update_post_meta(id, ‘key’, $data);?
   >
 * (maybe try entering null data and see if it solves the prob?)

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘update_post_meta problem saving field values’ is closed to new replies.

## Tags

 * [save](https://wordpress.org/support/topic-tag/save/)
 * [update_post_meta](https://wordpress.org/support/topic-tag/update_post_meta/)
 * [values](https://wordpress.org/support/topic-tag/values/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 4 replies
 * 3 participants
 * Last reply from: [MickR](https://wordpress.org/support/users/mickr/)
 * Last activity: [13 years, 3 months ago](https://wordpress.org/support/topic/update_post_meta-problem-saving-field-values/#post-3513988)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
