Good question! Try instead using ‘save_post’.

I was finding that this code:

$updated = update_post_meta($post_id, 'my_field', $html);

Was not saving the value to the database, when I checked, the field was empty, despite $html having content when the code fired.

This code was inside an update_post action:

add_action('post_updated', 'update_my_field');

When I updated it to:

add_action('save_post', 'update_my_field');

The value got correctly written. Note that ACF’s update_field() also failed to write the value inside post_updated.

I believe this is because the field is overwritten after post_updated.

Remember to check inside your function that the save_post instance is the right one, as it gets called multiple times.

For example, so avoid auto-saves, revisions and to only fire on post types when published or draft:

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    error_log ('Doing autosave, return early');

$current_post_id = get_the_ID();
if ( $post_id != $current_post_id ) {
    error_log ('Post_id does not match current post_id');

$post_type = get_post_type($post_id);
$post_status = get_post_status($post_id);

if ( $post_type == 'post' && ($post_status == 'publish' || $post_status == 'draft') ) {
    // do something
Last modified: October 26, 2023



Write a Reply or Comment

Your email address will not be published.