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' );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.