Carbon-fields: carbon_set_post_meta doesn't work inside save_post hook

Created on 16 May 2018  Â·  3Comments  Â·  Source: htmlburger/carbon-fields

Version

  • Carbon Fields: 2.2.0
  • WordPress: 4.9.5
  • PHP: 5.7

Expected Behavior

The post meta should be set after save_post_{custom_post_type} hook is fired

Actual Behavior

The post meta doesn't update

Strange Behavior

Put die(); next line after the carbon_set_post_meta function make the screen go white after click saves changes but once refresh the post meta is updated.

Container definition

// Auto Save Name
add_action('save_post_people','save_person_callback');
function save_person_callback($post_id){
  //Check it's not an auto save routine
   if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return;
  //Perform permission checks! For example:
  if ( !current_user_can('edit_post', $post_id) )
        return;
  carbon_set_post_meta( $post_id, 'person_first_name', 'My First Name' );
  // Add die(); here make the post meta updates but screen goes white
}

Steps to Reproduce the Problem

  1. Create custom post type
  2. Add the code above
  3. Create a new post and save

Most helpful comment

A bit late at the party, but you're doing it wrong. I mean not wrong-wrong, but you could do it better than going the $_POST route.

Instead of using save_post hook, you need to use the carbon_fields_post_meta_container_saved hook, that triggers just after the container is saved. You still have access to $_POST parameters if you need it.

https://github.com/htmlburger/carbon-fields/blob/61c7fd90cae6ac772e6492fcbb3759f70ff6fa5c/core/Container/Post_Meta_Container.php#L96:L108

All 3 comments

Ah, I ran into this too. After a lot of googling I found this answer on StackOverflow: https://wordpress.stackexchange.com/questions/243537/update-post-meta-not-saving-data-inside-of-save-post-filter/243560#243560

carbon_set_post_meta calls update_post_meta behind the scenes; but it turns out that save_post runs before the post is actually updated in the database (see below for qualification). So the values set via carbon_set_post_meta are overwritten by the blank form data in $_POST. If you die() in save_post, the post isn‘t actually saved — and whatever work you did in that hook stays put.

One solution is to simply modify the $_POST global in your hook instead, which feels icky but works. Note that Carbon Fields seems to prepend $_POST field names with an underscore, so:

carbon_set_post_meta( $post_id, 'person_first_name', 'My First Name' );

becomes

$_POST['_person_first_name'] = 'My First Name';

NB: The WordPress documentation claims that save_post runs _after_ the post is saved, though, contradicting these findings… which suggests that some other hook that writes to the database is running after save_post. I played around with different priorities on add_action, but the only solution I could find was to modify $_POST.

Maybe someone with a better understanding of WordPress can suggest a better way?

A bit late at the party, but you're doing it wrong. I mean not wrong-wrong, but you could do it better than going the $_POST route.

Instead of using save_post hook, you need to use the carbon_fields_post_meta_container_saved hook, that triggers just after the container is saved. You still have access to $_POST parameters if you need it.

https://github.com/htmlburger/carbon-fields/blob/61c7fd90cae6ac772e6492fcbb3759f70ff6fa5c/core/Container/Post_Meta_Container.php#L96:L108

@dvelyk You got it nearly right. save_post does run after the post is saved, but before the post_meta are saved. I think your carbon_set_post_meta() saves the post_meta values, then they get written over by the Carbon Fields save operation.

The suggestion by @iamntz works for me, you just have to replace save_post by carbon_fields_post_meta_container_saved and leave your function as it was. In both cases the first action parameter is $post_id.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bjoernhasse picture bjoernhasse  Â·  3Comments

jquimera picture jquimera  Â·  4Comments

leurdo picture leurdo  Â·  3Comments

jonwaldstein picture jonwaldstein  Â·  3Comments

Mick00 picture Mick00  Â·  3Comments