Support

Account

Home Forums General Issues save_post to update field if other field has changed

Unread

save_post to update field if other field has changed

  • I thought I got it all sorted, but it just doesn’t work the way I think it should.
    Upon saving a post, I want to check if the value of a field (‘zip’) has been changed (compared to the stored value), and then change the value of another field (‘status’) accordingly. This would be my function:

    function my_acf_save_post( $post_id ) {
    
    	// Get previous value
        $zip_old = get_field('zip', $post_id);
    
    	// Get value from input
    	$zip_new = $_POST['acf']['field_6463644299953'];
    
        if($zip_old != $zip_new) {
            $status = 'changed';
        } else {
    		$status = 'unchanged';
    	}
    
    	// Update field with $status message
    	update_field('status', $status, $post_id);
    }
    
    add_action('acf/save_post', 'my_acf_save_post', 5);

    From my understanding, this should detect if the value for the ‘zip’ field is different from the stored one and change the ‘status’ value. But even when I changed the ‘zip’ field value and saved the post, the ‘status’ still remains ‘unchanged’. Is there something in my logic that doesn’t make sense?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.