• Resolved ozgurgedikli

    (@ozgurgedikli)


    I am trying to filter the matches 4 days before and after in Marquee according to the leagues and show the date, time and score information, but sportspress cannot pull the data. Can anyone help with this?

     <div class="marquee-wrapper">
    <div class="container">
    <div class="marquee">
    <?php
    // SportPress query to capture matches
    $args = array(
    'post_type' => 'sp_event',
    'posts_per_page' => 50,
    'meta_query' => array(
    array(
    'key' => 'sp_date',
    'value' => array( date('Y-m-d', strtotime('-4 days')), date('Y-m-d', strtotime('+4 days')) ),
    'compare' => 'BETWEEN',
    'type' => 'DATE'
    ),
    ),
    );

    $events_query = new WP_Query($args);

    if ($events_query->have_posts()) :
    // We will pull match data in a loop
    while ($events_query->have_posts()) : $events_query->the_post();
    // We print the match title on the screen for testing
    echo 'Match Title: ' . get_the_title() . ' | ';

    // Let's get team names and scores
    $home_team = sp_get_team_name(get_the_ID(), 'home');
    $away_team = sp_get_team_name(get_the_ID(), 'away');
    $home_score = sp_get_team_result(get_the_ID(), 'home');
    $away_score = sp_get_team_result(get_the_ID(), 'away');
    $match_time = get_post_meta(get_the_ID(), 'sp_date', true);
    $league = wp_get_post_terms(get_the_ID(), 'sp_league', array('fields' => 'names'));

    // Let's show league, team names and scores for each match
    echo implode(', ', $league) . ': ' . $home_team . ' ' . $home_score . ' - ' . $away_team . ' ' . $away_score . ' (' . date('H:i', strtotime($match_time)) . ') | ';
    endwhile;

    wp_reset_postdata();
    else :
    // If there is no content, show this message
    echo 'There are no competitions with published schedules and results.';
    endif;
    ?>
    </div>
    </div>
    </div>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Savvas

    (@savvasha)

    Try to use the following $args:

    // SportPress query to capture matches
    $args = array(
    'post_type' => 'sp_event',
    'post_status' => array( 'published', 'future' ),
    'posts_per_page' => 50,
    'date_query' => array(
    array(
    'before' => date('Y-m-d', strtotime('+4 days')),
    'after' => date('Y-m-d', strtotime('-4 days') ),
    'inclusive' => true,
    ),
    ),
    );

    Thanks,
    Savvas

    Thread Starter ozgurgedikli

    (@ozgurgedikli)

    Yes, the data came with the date filter. Thanks @savvasha

    Currently, only the title of the matches is published as “team1 vs team2”.

    I need to work on the home away objects to pull the score if the match is ‘published’ and the match date and time if it is ‘in the future’.

    Plugin Contributor Savvas

    (@savvasha)

    Hi @ozgurgedikli ,

    You can take some ideas of how to retrieve and show SportsPress data from templates like event-list.php that can be found at wp-content/plugins/sportspress/templates/

    Thanks,
    Savvas

    Thread Starter ozgurgedikli

    (@ozgurgedikli)

    Hi @savvasha

    Thanks for the data details. I added the query to event-list.php to pull the match data from Sportspress

    $home_team = sp_get_team_name(get_the_ID(), 'home');
    $home_score = sp_get_team_result(get_the_ID(), 'home');

    $away_team = sp_get_team_name(get_the_ID(), 'away');
    $away_score = sp_get_team_result(get_the_ID(), 'away');

    $match_time = get_post_meta(get_the_ID(), 'sp_date', true);
    $match_date_readable = date_i18n(get_option('date_format'), strtotime($match_time));

    $league = wp_get_post_terms(get_the_ID(), 'sp_league', array('fields' => 'names'));

    I applied it for the Alchemists marquee display to pull it dynamically.

    <div class="marquee-wrapper">
    <div class="container">
    <div class="marquee">
    <div class="marquee-content">
    <?php
    // Query by date range
    $args = array(
    'post_type' => 'sp_event',
    'post_status' => array('publish', 'future'), // Both scheduled and broadcasted matches
    'posts_per_page' => 100, // Maximum matches
    'date_query' => array(
    array(
    'before' => date('Y-m-d', strtotime('+4 days')), // Up to 4 days later
    'after' => date('Y-m-d', strtotime('-4 days')), // Until 4 days ago
    'inclusive' => true,
    ),
    ),
    );

    // SportPress query to capture matches
    $events_query = new WP_Query($args);
    $matches_by_league = array(); // To group matches by leagues
    $processed_matches = array(); // To avoid showing the same match twice
    if ($events_query->have_posts()) :
    while ($events_query->have_posts()) : $events_query->the_post();

    // Get match ID
    $match_id = get_the_ID();

    // Get team names
    $home_team = sp_get_team_name($match_id, 'home');
    $away_team = sp_get_team_name($match_id, 'away');

    // Fetch post metadata for match time
    $match_time = get_post_meta($match_id, 'sp_date', true);

    // If the date information is empty or null, use a default information.
    if (empty($match_time)) {
    $match_time = null; // Default value: Unknown
    } else {
    $match_time = date('Y-m-d H:i', strtotime($match_time)); // Date format
    }

    // League information
    $league = wp_get_post_terms($match_id, 'sp_league', array('fields' => 'names'));

    // Check date format
    $formatted_time = $match_time ? date('d.m.Y H:i', strtotime($match_time)) : 'Bilinmiyor';

    // If there is league information, let's process it
    if (!empty($league)) {
    $league_output = implode(', ', $league);
    $home_score = function_exists('sp_get_team_result') ? sp_get_team_result($match_id, 'home') : null;
    $away_score = function_exists('sp_get_team_result') ? sp_get_team_result($match_id, 'away') : null;

    // Generate a unique key: Using the match ID we prevent the same match from being added twice
    $match_key = $match_id;

    // Check to avoid adding the same match twice
    if (!in_array($match_key, $processed_matches)) {
    $is_past = $match_time && strtotime($match_time) < time(); // Check if the match is past or future

    // Only show as played if the score is full and the match is in the past
    if ($is_past && $home_score !== null && $away_score !== null) {

    // If the match has been played, show the score
    $matches_by_league[$league_output][] = $home_team . ' ' . $home_score . ' - ' . $away_score . ' ' . $away_team;
    } else if (!$is_past) {
    // If in the future and there is no score, show match details
    $matches_by_league[$league_output][] = $home_team . ' vs ' . $away_team . ' (' . $formatted_time . ')';
    }

    // Add match to processed list
    $processed_matches[] = $match_key;
    }
    }
    endwhile;
    wp_reset_postdata();
    endif;

    // Show matches by league
    if (!empty($matches_by_league)) {
    foreach ($matches_by_league as $league => $matches) {
    echo $league . ': '; // Show league name
    echo implode(' | ', $matches) . ' | ';
    }
    } else {
    echo 'The weekly program was not published.';
    }
    ?>
    </div>
    </div>
    </div>
    </div>

    Flowing text on the marquee

    2.Amateur League (Play-Off): Team2 vs Team1 vs Team2 vs Team1 (Unknown) | Team1 vs Team2 vs Team1 vs Team2 (Unknown)

    The text planned to flow on the marquee

    2.Amateur League (Play-Off): Team2 5 – 4 Team1 | Team1 vs Team2 (09 October 2024 18:30)

    Matches are repeated twice in Marquee and it is not possible to pull the score and date information.
    What could be the reason for this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.