Support

Account

Home Forums General Issues Update field on custom post delete

Solved

Update field on custom post delete

  • I want to update a field when a custom post type is being deleted. Right now I have a relationship field “trainees” as part of a training course field group. The “trainees” field populates values from a Trainee custom post type. I calculate training places left with the below code whenever a training course is updated:

    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);

    The remaining training courses is a read-only field in the backend that gets calculated.

    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.

    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:

    function on_publish_trash_post( $post ) {
        if ($post->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->ID);
    }
    
    add_action(  'publish_to_trash',  'on_publish_trash_post', 10, 1 );
    

    What am I doing wrong?

  • Found the solution for anyone interested:

    function gm_trash_trainee( $post_id ) {
        $post_type = get_post_type( $post_id );
        $post_status = get_post_status( $post_id );
        if ( $post_type == 'trainee' ) {
    	  
    		$args = array( 'post_type' => 'training_course');
    
    		$loop = new WP_Query( $args );
    		while ( $loop->have_posts() ) : $loop->the_post();
    	  		$number_of_places_available = get_field('number_of_places_available', the_ID());
    			$number_of_trainees = count(get_field('trainees', the_ID()));
    	  		$number_of_places_left = $number_of_places_available - $number_of_trainees;
    	  		update_field('number_of_places_left', $number_of_places_left, the_ID());
        
    		endwhile;
    	}
    }
    add_action( 'trashed_post', 'gm_trash_trainee' );
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Update field on custom post delete’ is closed to new replies.