update_post_meta doesn’t 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)
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.