I'm trying to update a custom field by hooking into the save-post action, but for reasons I can't figure out, it's not working.

The following function is placed in the theme's functions.php:

function save_address_meta() {

      $meta = get_post_meta( get_the_ID() );

      $address = $meta['address'];
      update_post_meta(get_the_ID(), $address, 'test');
add_action( 'save_post', 'save_address_meta', 50 );

I've tried using pre_post_update as well, as I understand that save_post won't actually fire unless something, other than a custom field, is updated in the post - but no luck with this one either.

I've spent a few hours searching for solutions on stackexchange and various other sources online, but just not coming right. This is a dumbed-down version of the original code, but even in this basic state it doesn't appear to be working.

Basically, I'm trying to get the custom field in question, then update it with a string value.

If I print_r the $meta array, the custom field value appears in an array as follows:

 [address] => Array ( [0] => 50 Call Lane Leeds LS1 6DT United Kingdom )

I've also tried accessing this custom field in the function above using $address = $meta['address'][0].
I can echo out the value of the key this way, but if I'm not mistaken, it's the key I'll need to reference in order for the string, in the 3rd argument, to update the value as intended.

4 Answers 4


Try changing update_post_meta(get_the_ID(), $address, 'test'); to update_post_meta(get_the_ID(), 'address', 'test');

  • This was the issue in my case, a simple "syntax" error. Do you know why using a string would work instead of a variable? Mar 26, 2017 at 22:09
  • The second argument for update_post_meta() function is meta_key . In your case meta_key is address Mar 26, 2017 at 22:11
  • However, As Nathan suggested, Its better to use some check (whether its a revision or autosave, or if is the same post type you need) to make sure your action is fired at the perfect time. Mar 26, 2017 at 22:13

The save_post hook will fire whenever WordPress saves a post to the database. This includes saving WP revisions which will have a different post ID than the actual post. It's more than likely you are getting and saving the post meta to a revision instead of to the actual post.

Also, the save_post hook passes a few variables when it fires, including the post ID, so you don't have to use the get_the_ID() function.

function wpse_261414_save_post( $post_id, $post, $update ) {
  //* Make sure this isn't a post revision
  if( wp_is_post_revision( $post_id ) ) {
  $meta = get_post_meta( $post_id );
  $address = $meta[ 'address' ];
  update_post_meta( $post_id, $address, 'test' );
add_action( 'save_post', 'wpse_261414_save_post', 10, 3 );
  • You are right about save-post and when it fires. Thanks for the tips, I've ditched the get_the_ID() function in favour of the $post_id variable passed by save_post. turns out it's not saving the post meta to a revision, but I've kept that conditional check in there anyway for good measure. Mar 26, 2017 at 22:06

Why don't you try something like this;

function save_address_meta() {
    global $post;
    if($post->post_type == 'your-custom-post-type'){
        $address_field = 'test'; //Get your address field here                  
        update_post_meta($post->ID, 'address', 'test');
add_action( 'save_post', 'save_address_meta' );

And you can get your field value elsewhere may be for example inside the loop as;

$address = get_post_meta($post->ID, 'address', true);

I had these symptoms, but it turned out to be a validation check behaving unexpectedly, namely:

if( !current_user_can( 'edit_post') ) return;

This had been working (for several years), but a Wordpress update caused it to fail.

I amended it to also take the $post_id (as per the documentation example here):

if( !current_user_can( 'edit_post', $post_id) ) return;

and all was well. So in case this helps anyone else...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.