Search code examples
wordpressadvanced-custom-fieldsacfpro

Delete ACF Value Based off another value


I am hoping someone could please assist with the below snippet, I need for one of my fields values to be deleted when a different select field is changed to complete.

add_action('acf/save_post', 'my_acf_save_post');
function my_acf_save_post($post_id){

$status = get_field('status');
    if ('complete', $status){
        delete_field('project_address');
  }
}

Solution

  • You need to pass the "post_id" to select the value and delete the value. Logic it true, I think.

    add_action('acf/save_post', 'my_acf_save_post');
    function my_acf_save_post( $post_id ){
    
    $status = get_field( 'status', $post_id );
      if ( 'complete' === $status ){
          delete_field( 'project_address', $post_id );
      }
    }
    

    Hope it help.