Topic: How to Retrieve Matches with Complete Team Statistics as JSON in WordPres
-
Hello everyone,
I’m currently working on a project that involves managing football match data using the SportsPress plugin on WordPress. My goal is to retrieve a JSON object that contains all the upcoming matches, along with the complete statistics for each team involved in these matches.What I Need:
- A JSON object with the following structure:
- Matches: Each match should have details like match ID, title, date, and a nested array of teams.
- Teams: Each team should include details like team ID, name, permalink, thumbnail URL, and full statistics (goals scored, goals against, wins, losses, draws).
Example JSON Structure:
Example JSON Structure:
[
{
"match_id": 238,
"match_title": "At. Tucumán vs Boca Jrs.",
"match_date": "2024-05-21 00:00:00",
"teams": [
{
"id": 33,
"title": "At. Tucumán",
"thumbnail": "https://localhost/futbol/wp-content/uploads/2024/06/tucuman.png",
"permalink": "https://localhost/futbol/team/at-tucuman/",
"statistics": {
"goals": 30,
"goals_against": 20,
"wins": 10,
"losses": 5,
"draws": 5
}
},
{
"id": 30,
"title": "Boca Jrs.",
"thumbnail": "https://localhost/futbol/wp-content/uploads/2024/06/boca.png",
"permalink": "https://localhost/futbol/team/boca-jrs/",
"statistics": {
"goals": 40,
"goals_against": 10,
"wins": 12,
"losses": 3,
"draws": 5
}
}
]
}
]What I Have Tried:
I have managed to retrieve the matches and basic team information using the following code snippet in my
functions.php
:function get_post_thumbnail_url($post_id, $size = 'post-thumbnail') {
$thumbnail_id = get_post_thumbnail_id($post_id);
if ($thumbnail_id) {
$thumbnail = wp_get_attachment_image_src($thumbnail_id, $size);
if ($thumbnail) {
return $thumbnail[0];
}
}
return false;
}
function get_match_results($match_id) {
$results_meta = get_post_meta($match_id, 'sp_results', true);
$results = maybe_unserialize($results_meta);
return $results;
}
function get_team_statistics($team_id) {
$args = array(
'posts_per_page' => -1,
'post_type' => 'sp_event',
'meta_query' => array(
array(
'key' => '_sp_team',
'value' => '"' . $team_id . '"',
'compare' => 'LIKE'
)
)
);
$matches = get_posts($args);
$team_stats = array(
'goals' => 0,
'goals_against' => 0,
'wins' => 0,
'losses' => 0,
'draws' => 0
);
foreach ($matches as $match) {
$results = get_match_results($match->ID);
if ($results) {
foreach ($results as $team_key => $result) {
if ($team_key == $team_id) {
if (isset($result['goals'])) {
$team_stats['goals'] += intval($result['goals']);
}
if (isset($result['outcome'])) {
foreach ($result['outcome'] as $outcome) {
if ($outcome == 'win') {
$team_stats['wins'] += 1;
} elseif ($outcome == 'loss') {
$team_stats['losses'] += 1;
} elseif ($outcome == 'draw') {
$team_stats['draws'] += 1;
}
}
}
} else {
if (isset($result['goals'])) {
$team_stats['goals_against'] += intval($result['goals']);
}
}
}
}
}
return $team_stats;
}
function get_teams_by_league($league_name) {
$league = get_term_by('name', $league_name, 'sp_league');
if (!$league) {
return [];
}
$args = array(
'posts_per_page' => -1,
'post_type' => 'sp_team',
'tax_query' => array(
array(
'taxonomy' => 'sp_league',
'field' => 'term_id',
'terms' => $league->term_id,
),
),
);
$teams = get_posts($args);
$team_data = [];
if ($teams) {
foreach ($teams as $team) {
$team_stats = get_team_statistics($team->ID);
$team_data[] = array(
'id' => $team->ID,
'title' => $team->post_title,
'thumbnail' => get_post_thumbnail_url($team->ID, 'thumbnail'),
'permalink' => get_post_permalink($team->ID),
'statistics' => $team_stats,
);
}
}
return $team_data;
}
function get_upcoming_matches_with_teams_and_statistics($hours = 48) {
$current_time = current_time('mysql');
$future_time = date('Y-m-d H:i:s', strtotime("+$hours hours", strtotime($current_time)));
$args = array(
'post_type' => 'sp_event',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'sp_results',
'compare' => 'EXISTS',
),
),
'date_query' => array(
array(
'after' => $current_time,
'before' => $future_time,
'inclusive' => true,
),
),
);
$query = new WP_Query($args);
$matches_data = array();
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$event_id = get_the_ID();
$event_title = get_the_title();
$event_date = get_the_date('Y-m-d H:i:s');
$teams_meta = get_post_meta($event_id, '_sp_team', true);
$teams = maybe_unserialize($teams_meta);
$team_details = array();
if (!empty($teams)) {
foreach ($teams as $team_id) {
$team_post = get_post($team_id);
$team_stats = get_team_statistics($team_id);
$team_details[] = array(
'id' => $team_id,
'title' => $team_post->post_title,
'thumbnail' => get_post_thumbnail_url($team_id, 'thumbnail'),
'permalink' => get_post_permalink($team_id),
'statistics' => $team_stats,
);
}
}
$matches_data[] = array(
'match_id' => $event_id,
'match_title' => $event_title,
'match_date' => $event_date,
'teams' => $team_details,
);
}
wp_reset_postdata();
}
return json_encode($matches_data);
}
// Function to test and display the JSON data
function test_display_matches_with_teams_and_statistics() {
$matches_json = get_upcoming_matches_with_teams_and_statistics();
echo '<pre>';
echo $matches_json;
echo '</pre>';
}
add_action('wp_footer', 'test_display_matches_with_teams_and_statistics');Issue:
The JSON object returned does not include complete team statistics. It seems the statistics are either missing or set to zero.Request for Help:
Could anyone provide guidance on how to correctly retrieve and include the complete statistics for each team in the JSON output? Specifically, I need to ensure the statistics such as goals scored, goals against, wins, losses, and draws are accurately populated.
Any insights, code snippets, or documentation references would be greatly appreciated!
Thank you in advance for your help!
- A JSON object with the following structure:
- You must be logged in to reply to this topic.