Skip to content

Instantly share code, notes, and snippets.

@marktenney
Created March 14, 2021 13:53
Show Gist options
  • Save marktenney/7acf2f24f88de9b78490c8f2a17e085d to your computer and use it in GitHub Desktop.
Save marktenney/7acf2f24f88de9b78490c8f2a17e085d to your computer and use it in GitHub Desktop.
Use the max_teams value to determine how many sp_teams can be added to a league (cpt)
<?php
// changing the league status
add_action('updated_post_meta', 'change_league_status', 0, 4);
function change_league_status($meta_id, $post_id, $meta_key, $meta_value) {
$active_teams = get_post_meta( $post_id, 'active_teams' ); // getting active teams id's
$max_teams = (int)get_post_meta( $post_id, 'max_teams' )[0]; // getting number of max teams
$active_teams_length = count($active_teams); // getting active teams length
if($active_teams_length === $max_teams){
update_post_meta( $post_id, 'league_open', 'closed'); //converting league status
}
if($active_teams_length < $max_teams){
update_post_meta( $post_id, 'league_open', 'open'); //converting league status
}
if($active_teams_length > $max_teams){
$teams_differance = $active_teams_length-$max_teams; //getting differance
$reverse_array = array_reverse($active_teams);
for ($i = 0; $i < $teams_differance ; $i++ ){ //deleting extra teams
delete_post_meta( $post_id, 'active_teams', $reverse_array[$i] );
}
update_post_meta( $post_id, 'league_open', 'closed'); //converting league status
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment