Home Forums Add-ons Repeater Field Update sub field before/after save post


Update sub field before/after save post

  • Expected behavior:

    When user updates an ACF in the WP dashboard and hits UPDATE, code will check for updated Status field and if values have changed, will also modify Last Updated field, then save all information.


    The overall content is updated but the Last Updated field is not affected by the update_sub_field code. However, if I remove the return $post line at the end, the Last Updated field is saved, but the overall content reverts back to the original values (not saved).

    I need both to be saved! I have a feeling that things are just not happening in the right order and I can accomplish this if I can just figure out the proper filter/action to run, but so far none of these have worked with the below code (have tried different priorities as args, as well):

    add_action( 'acf/save_post' , 'update_post');
    add_action( 'save_post' , 'update_post');
    add_filter( 'wp_insert_post_data' , 'update_post');

    Any help is greatly appreciated. Thank you!

    function update_post( $post ) {
        $projectCount = -1;
    	if( have_rows('project') ):
    		while( have_rows('project') ): the_row();
    			$taskCount = -1;
    			if( have_rows('project_issue') ):
    				while( have_rows('project_issue') ): the_row();
    					$oldValue 	= get_sub_field('project_current_status');
    					$newValue 	= $_POST['acf']['field_594afbf65e909'][$projectCount]['field_594afd675e90d'][$taskCount]['field_594c05c666a9f'];
    					$date = date('Ymd');
    					$updated = ($oldValue !== $newValue) ? 'true' : 'false';
    					if ( $updated == 'true' ) { 
    						update_sub_field('project_last_update', $date);
    	return $post;
  • In case anyone ever reads this, solved with:

    add_action( 'update_post_meta', 'update_post', 10, 4 );

    …and removing the return at the end.

  • Perhaps someone can tell me why this code:

    $oldValue = get_sub_field('project_current_status');

    …is returning the last of all of the ‘project_current_status’ fields (total)!

    My understanding is that it should only be returning the field referenced inside of the ‘have_rows’ loop.

    Is this not correct? Thank you.

  • Looking back over your comment, to be honest I’m not sure where the problem is. However, if I was going to update a field associated with ACF and I needed to look at old values vs new values

    add_action('acf/save_post', 'update_post', 1);

    Setting the priority of 1 means that your function will run before ACF saves the new values. This means that you can use the ACF functions have_rows() and get_sub_field() to get the old values and $_POST['acf'] for the new values.

    In your OP you indicated that you tried this

    add_action( 'acf/save_post' , 'update_post');

    This sets the priority to 10, which is the same as when ACF runs it’s save. There isn’t any way to guarantee under this condition which will run first, your function or ACF.

    Looking at your last comment, you need to supply the post ID for the initial have_rows() calls in your filter so that ACF knows what post to look at.

    function update_post( $post_id ) {
        $projectCount = -1;
    	if( have_rows('project', $post_id) ):
    		while( have_rows('project', $post_id) ): the_row();
    // after this you don't need to add $post_id for any sub fields

    Hope that helps

  • This is immensely helpful, thank you so much for lending a hand. =)

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

You must be logged in to reply to this topic.