Search code examples
wordpressadvanced-custom-fieldsacfpro

ACF update field to null on field change


I am hoping someone could please assist, I need to delete a field when a status goes to complete for a post, below is the code, right now this works but it always changes the field to null when the post updates it doesn’t matter what the status is. I need it to only delete the value when the status is complete.

add_action( 'acf/save_post', 'my_acf_save_post_7',10 );
function my_acf_save_post_7( $post_id ) {
$value = get_field( 'status', $post_id );
if ( $value = 'complete' ) {
    update_field( 'project_address_1', null, $post_id );
}
}

Solution

  • Can you try this

    ACF Field - https://www.advancedcustomfields.com/resources/delete_field/

    delete_field( 'my_field', $post_id );
    

    Like delete_field( 'color', 123 );

    Post meta - https://developer.wordpress.org/reference/functions/delete_post_meta/