Getting team stats in Sports Bench

So there are a lot of ways to easily get player stats in Sports Bench, but there aren’t many options for team stats.

Of course, you could write your own MySQL to get those stats, but that’s not really a valid option if you’re not good with code. Fortunately, the latest version of Sports Bench helps to fix this issue.

So here’s how you can get team stats to display on your site.

Function to get stats

New for Sports Bench 1.7 is a function that will get teams stats for you for a given season. Prior to the latest update, you would have had to run your own MySQL to get the team stats.

Now, all you have to do is call the sports_bench_get_team_season_stats function. This takes in a Sports_Bench_Team object and the season for the stats you’re looking for, and it returns array of the stats. The stats are all of the stats from the sb_games table plus the opponents goals/points/runs scored.

Once you’ve got the array, you can do whatever you want with it.

Team stats filter

Also new for Sports Bench in the latest update is the team stats table that now shows on the single team pages. This makes it easy to display team stats since you don’t have to do anything to get them to show.

As with everything, there is a filter involved in showing this table. It’s the sports_bench_team_season_stats filter, and it’s pretty easy to use.

This filter takes in a Sports_Bench_Team object and a season for the stats to retrieve. You do have to call the sports_bench_get_team_season_stats function inside of the filter though. But otherwise it’s not too hard to use.

Here’s a soccer example to help get you going.

function sports_bench_example_team_season_stats( $html, $team, $season) {
$stats_array = sports_bench_get_team_season_stats( $team, $season );
$total_stats = $stats_array[ 0 ];
$average_stats = $stats_array [ 1 ];
$html .= '<table class="sports-bench-team-stats-table">';

$html .= '<thead>';
$html .= '<tr>';
$html .= '<th></th>';
$html .= '<th>' . __( 'Total Stats', 'sports-bench' ) . '</th>';
$html .= '<th>' . __( 'Average Stats', 'sports-bench' ) . '</th>';
$html .= '</tr>';
$html .= '</thead>';

$html .= '<tbody>';
$html .= '<tr>';
$html .= '<td class="left">' . __( 'Goals', 'sports-bench' ) . '</td>';
$html .= '<td>' . $total_stats[ 'goals' ] . '</td>';
$html .= '<td>' . $average_stats[ 'goals' ] . '</td>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td class="left">' . __( 'Possession', 'sports-bench' ) . '</td>';
$html .= '<td>' . $total_stats[ 'possession' ] . '</td>';
$html .= '<td>' . $average_stats[ 'possession' ] . '</td>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td class="left">' . __( 'Shots', 'sports-bench' ) . '</td>';
$html .= '<td>' . $total_stats[ 'shots' ] . '</td>';
$html .= '<td>' . $average_stats[ 'shots' ] . '</td>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td class="left">' . __( 'Shots on Goal', 'sports-bench' ) . '</td>';
$html .= '<td>' . $total_stats[ 'sog' ] . '</td>';
$html .= '<td>' . $average_stats[ 'sog' ] . '</td>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td class="left">' . __( 'Corners', 'sports-bench' ) . '</td>';
$html .= '<td>' . $total_stats[ 'corners' ] . '</td>';
$html .= '<td>' . $average_stats[ 'corners' ] . '</td>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td class="left">' . __( 'Offsides', 'sports-bench' ) . '</td>';
$html .= '<td>' . $total_stats[ 'offsides' ] . '</td>';
$html .= '<td>' . $average_stats[ 'offsides' ] . '</td>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td class="left">' . __( 'Fouls', 'sports-bench' ) . '</td>';
$html .= '<td>' . $total_stats[ 'fouls' ] . '</td>';
$html .= '<td>' . $average_stats[ 'fouls' ] . '</td>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td class="left">' . __( 'Saves', 'sports-bench' ) . '</td>';
$html .= '<td>' . $total_stats[ 'saves' ] . '</td>';
$html .= '<td>' . $average_stats[ 'saves' ] . '</td>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td class="left">' . __( 'Goals Against', 'sports-bench' ) . '</td>';
$html .= '<td>' . $total_stats[ 'goals_against' ] . '</td>';
$html .= '<td>' . $average_stats[ 'goals_against' ] . '</td>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td class="left">' . __( 'Yellow Cards', 'sports-bench' ) . '</td>';
$html .= '<td>' . $total_stats[ 'yellows' ] . '</td>';
$html .= '<td>' . $average_stats[ 'yellows' ] . '</td>';
$html .= '</tr>';

$html .= '<tr>';
$html .= '<td class="left">' . __( 'Red Cards', 'sports-bench' ) . '</td>';
$html .= '<td>' . $total_stats[ 'reds' ] . '</td>';
$html .= '<td>' . $average_stats[ 'reds' ] . '</td>';
$html .= '</tr>';
$html .= '</tbody>';

$html .= '</table>';
return $html;
}
add_filter( 'sports_bench_team_season_stats', 'sports_bench_example_team_season_stats', 10, 3 );

So there you have it. You can now get and display team stats on your website no code or all the code you like.