Title: update_post_meta doesn&#8217;t work with variable key/values?
Last modified: March 14, 2018

---

# update_post_meta doesn’t work with variable key/values?

 *  Resolved [dbirlew](https://wordpress.org/support/users/dbirlew/)
 * (@dbirlew)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-doesnt-work-with-variable-key-values/)
 * To avoid adding post meta values one at a time (and reduce repeat code in my 
   theme) I wrote a simple class to store all the options so I can iterate through.
   The class works fine and constructs an object that I’m able to add valid options
   to (in much the same format one adds controls to the Customizer screen), and 
   output the options forms in the meta box, but the form values won’t save as post
   meta.
 * Here’s how I’m trying to save the options, with some notes after. Please let 
   me know if you have any ideas or need more info:
 *     ```
       add_action( 'save_post', 'save_theme_options' );
       function save_theme_options( $post_id ) {
           global $theme_options;
           foreach ( $theme_options->options as $option ) {
               if ( array_key_exists( $option->id, $_POST ) ) {
                   update_post_meta($post_id, $option->meta_key, $_POST[$option->id]);
               }
           }
       }
       ```
   
 * $option->id is actually a text value in my object, such as ‘example_option’.

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

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-doesnt-work-with-variable-key-values/#post-10076049)
 * Nothing looks to out of place in your code, but without knwoing the code for 
   the options object, it’s a bit harder to say. The only thing that stands out 
   is that you’re testing to se if $option->id exists, but you’re updating using
   $option->meta_key so that could be an issue.
 * What debugging have you done? It if was me, I’d add in something like this, so
   I can be sure of what’s actually being processed in each option…
 *     ```
       update_post_meta($post_id, $option->meta_key, $_POST[$option->id]);
       echo "<p>Updating post '".$post_id."', option id '".$option->id."' with key: '".$option->meta_key."', with value '".$_POST ['$option->id']."' - for final meta value of '".get_post_meta ($post_id, $option->met_key, true."'</p>";
       ```
   
 * That will show you all of the values that you’re working with, and take any guess
   work away.
 *  Thread Starter [dbirlew](https://wordpress.org/support/users/dbirlew/)
 * (@dbirlew)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-doesnt-work-with-variable-key-values/#post-10080034)
 * Thanks [@catacaustic](https://wordpress.org/support/users/catacaustic/) but unfortunately
   this is called on the save_post action, so the page reloads before any additional
   strings can be displayed.
 * UPDATE: I managed to save the values! Unfortunately, I did it by re-instantiating
   the object within my save_theme_options function, adding the options back to 
   it again, and updating the page.
 * So the problem is that attempting to carry the already existing object into save_post
   isn’t working, for some reason, even though it accepts the $post_id quite readily.
   Might be a simple PHP syntax solution, so I’ll research more along those lines…
   unless anyone can shed light.
 * Thanks!
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-doesnt-work-with-variable-key-values/#post-10080057)
 * The way to see those values is to add
 * `die();`
 * right after you do the processing. That will stop everything else, including 
   the redirect, and show you what you’re dealing with. 🙂
 * From what you’ve said here, it sounds like the values aren’t being set in your
   object before you try to save them. If that’s the case you’ll need to look further
   downstream to find out where they should be getting set and work from there.
 *  Thread Starter [dbirlew](https://wordpress.org/support/users/dbirlew/)
 * (@dbirlew)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-doesnt-work-with-variable-key-values/#post-10080092)
 * [@catacaustic](https://wordpress.org/support/users/catacaustic/): Ah! Makes sense,
   I’ve only ever used die() in a limited fashion.
 * My mistake was that even though I was instantiating the object outside of any
   function, I was adding all my individual option objects to the managing object
   within a function. 😛 Moving that code outside of a function worked, as the managing
   object remained blank to all except the single function.
 * I’ll need to reorganize code, and quite a bit, but I think I can make this work
   now! 😀

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

The topic ‘update_post_meta doesn’t work with variable key/values?’ is closed to
new replies.

## Tags

 * [$_POST](https://wordpress.org/support/topic-tag/_post/)
 * [save_post](https://wordpress.org/support/topic-tag/save_post/)
 * [update_post_meta](https://wordpress.org/support/topic-tag/update_post_meta/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 2 participants
 * Last reply from: [dbirlew](https://wordpress.org/support/users/dbirlew/)
 * Last activity: [8 years, 2 months ago](https://wordpress.org/support/topic/update_post_meta-doesnt-work-with-variable-key-values/#post-10080092)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
