title: WordPress Advanced Custom Fields: Update field on custom post delete
body: <p>I want to update a field when a Custom Post Type (CPT) is being deleted. Right now I have a relationship field <em>trainees</em> as part of a training course field group. The <em>trainees</em> field populates values from a Trainee CPT.</p>
  
  <p>I calculate training places left with the below code, whenever a training course is updated:</p>
  
  <pre><code>function my_acf_save_post( $post_id ) {
  
  // get new value
  $number_of_places_available = get_field('number_of_places_available');
  $number_of_trainees = count(get_field('trainees')); 
  if ( empty(get_field('trainees')) ) {
    $number_of_places_left = $number_of_places_available;
  } else {
    $number_of_places_left = $number_of_places_available - $number_of_trainees;
  }
  
  
  // do something
  update_field('number_of_places_left', $number_of_places_left, $post_id);
  }
  
  add_action('acf/save_post', 'my_acf_save_post', 20);  
  </code></pre>
  
  <p>The remaining training courses is a read-only field in the backend that gets calculated.</p>
  
  <p>What I want to do is update the remaining training places left when a trainee is actually deleted (moved to trash). Currently, when I delete a trainee, I would have to save the training course, the trainee belongs to for the remaining training places to update correctly. I would like to update this field when a trainee is actually deleted without me having to save the training course.</p>
  
  <p>I am attempting to use post status transitions to do that, but it doesn’t seem to be working, meaning that when I refresh the training course at the backend, even though the trainee has been removed from the relationship field, the number of available places has not been updated and only gets updated when I save the training course:</p>
  
  <pre><code>function on_publish_trash_post( $post ) {
      if ($post-&gt;post_type != 'trainee'){
          return;
      }
  
  // get new value
  $number_of_places_available = get_field('number_of_places_available');
  $number_of_trainees = count(get_field('trainees'));
  if ( empty(get_field('trainees')) ) {
    $number_of_places_left = $number_of_places_available;
  } else {
    $number_of_places_left = $number_of_places_available - $number_of_trainees;
  }
  
  // do something
  update_field('number_of_places_left', $number_of_places_left, $post-&gt;ID);
  }
  
  add_action(  'publish_to_trash',  'on_publish_trash_post', 10, 1 );
  </code></pre>
  
  <p>What am I doing wrong?</p>