Support

Account

Home Forums General Issues Update field on custom post delete Reply To: Update field on custom post delete

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