• g6063523

    (@g6063523)


    im creating a code that automatically assigns players to the match .
    however no matter how much i change the code i keep getting the same results.
    option 1.
    Team 1
    Gets Team 1 Players (Unselected)
    Gets Team 2 Players (Selected)
    ——–
    Team 2
    Gets Team 2 Players(Selected)

    when i try changing the code a bit ill get something like
    option 2
    Team 1
    Gets Team 1 Players (Unselected)
    Gets Team 2 Players (Selected)
    ————-
    Team 2
    Gets Team 1 Players (Selected)
    Gets Team 2 Players (Selected)

    ill provide option 1 code. and let me know if im doing something wrong or if you believe theres a betteer approach.
    below is the code. Please Help

    // Assign players to match
    function assign_players_to_match($match_id, $team_ids) {
        if (count($team_ids) < 2) {
            return;
        }
    
        $team_1_id = $team_ids[0];
        $team_2_id = $team_ids[1];
    
        // Clear current players
        delete_post_meta($match_id, 'sp_player');
    
        // Add players to team 1
        $team_1_players = get_players_by_team_id($team_1_id);
        foreach ($team_1_players as $player_id) {
            add_post_meta($match_id, 'sp_player', $player_id, false);
        }
    
        // Clear players from team 2 again
        delete_post_meta($match_id, 'sp_player');
    
        // Add players to team 2
        $team_2_players = get_players_by_team_id($team_2_id);
        foreach ($team_2_players as $player_id) {
            add_post_meta($match_id, 'sp_player', $player_id, false);
        }
    }
    
    function get_players_by_team_id($team_id) {
        $args = array(
            'post_type' => 'sp_player',
            'posts_per_page' => -1,
            'post_status' => 'publish',
            'fields' => 'ids',
            'meta_query' => array(
                array(
                    'key' => 'sp_current_team',
                    'value' => $team_id,
                    'compare' => '=',
                ),
            ),
        );
    
        $player_query = new WP_Query($args);
        return $player_query->posts;
    }
    
  • The topic ‘Players Pushed To Matches.’ is closed to new replies.