The post meta should be set after save_post_{custom_post_type} hook is fired
The post meta doesn't update
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.
// 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
}
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.
@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.
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
$_POSTroute.Instead of using
save_posthook, you need to use thecarbon_fields_post_meta_container_savedhook, that triggers just after the container is saved. You still have access to$_POSTparameters if you need it.https://github.com/htmlburger/carbon-fields/blob/61c7fd90cae6ac772e6492fcbb3759f70ff6fa5c/core/Container/Post_Meta_Container.php#L96:L108