Support

Account

Home Forums General Issues Update_field not working on post update

Solved

Update_field not working on post update

  • Using the transition_post_status, I can’t get update_field to save here. Everything else in the function works. For instance, if I uncomment the two lines that are currently commented, I get 13. What am I missing here?

    function do_something_on_save( $new_status, $old_status, $post ) {
        
        //echo get_the_ID(); //returns 13
        //exit;
    
        update_field(
            'notification_record',
            'checking',
            13
        );
    }
    add_action( 'transition_post_status', 'do_something_on_save', 10, 3 );
  • Try using the field key instead of the field name. ACF cannot update fields using the field name for fields that do not already have values.

  • @hube2 Thanks for responding 🙂

    I added the following:

    
    $field = get_field_object('notification_record');
    var_dump($field["key"]);
    exit;
    

    which gave me ‘field_57cee83c43f92’ as the key

    so I updated my code here and it’s still not saving 🙁

    
    function do_something_on_save( $new_status, $old_status, $post ) {
        
        //echo get_the_ID(); //returns 13
        //exit;
    
        //$field = get_field_object('notification_record');
        //var_dump($field["key"]); //returns 'field_57cee83c43f92'
        //exit;
    
        update_field(
            'field_57cee83c43f92',
            'checking',
            13
        );
    }
    add_action( 'transition_post_status', 'do_something_on_save', 10, 3 );
    
  • Are you sure the post you are updating has an ID of 13? What type of field are you attempting to update? Is the field a sub field of a repeater or flexible content field? Where are you trying to see if it’s been updated?

  • I switched to the ‘acf/save_post’ hook instead of ‘transition_post_status’, per a support ticket, and it’s saving now 🙂

    I got the post id by echoing out get_the_ID() (see code comments); I just hardcoded it to be sure. This was just a basic text field. To test, I was just saving the post and checking to see that the field had been updated from the edit-post view.

    Note: For this to work, I set the priority to 20; leaving it as 1 (before post save) didn’t work.

    Thanks for the help.

  • I have the same issue – ACF functions do not seem to work in update_post, regardless of priority.

    The solution for me, because I needed to use post_updated to compare old and new post titles and slugs, was to update the value in $_POST[‘acf’]. E.g:

    $_POST['acf']['field_5f19d4ad03a30'][] = ['field_5f19d4b503a31' => $old_title]

    In this case, I am saving the old title to a repeater. For this to work you must use field keys, not field names.

  • Howdy! Could you perhaps attach what your final code looked like? I am having the same problem and cannot get this to work!

  • Try using the native WP function update_post_meta instead, unless you’re sure ACF has loaded at the time you’re trying to use the function:

    update_post_meta( $postid, ‘ReleaseY’, $json->Year );
    Reference: https://developer.wordpress.org/reference/functions/update_post_meta/

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Update_field not working on post update’ is closed to new replies.