Forum Replies Created

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter granedoy

    (@granedoy)

    [BUG FIX] Midseason Transfers counting games from wrong team

    Hi everyone,

    I want to share a bug I found in SportsPress Pro 2.7.26/2.7.29 related to the Midseason Transfers module, and the fix I applied in case anyone else runs into the same issue.

    The Problem

    When a player is transferred mid-season, their statistics table incorrectly includes games played before the transfer date as games for the new team.

    Example: A player played 2 games for Team A, then transferred to Team B on May 6. After the transfer, the stats table showed 2 games for Team B instead of 1.

    All the data was correct (event dates, transfer date, boxscores), but the filtering was broken.

    Root Cause

    The bug is in this file:
    wp-content/plugins/sportspress-pro/includes/sportspress-midseason-transfers/sportspress-midseason-transfers.php

    In the event_args() function, when a team is selected ($selected_team != -1), the function returns immediately after adding the team filter — without applying the date_from filter. This means all games where the player appeared for that team are counted, regardless of the transfer date.

    Additionally, there is a typo in the $date_from && $date_to block where $date_from is used twice instead of $date_from and $date_to.

    The Fix

    Replace the entire event_args() function with the following:

    public function event_args( $args = array(), $data = array(), $season_id = 0, $selected_team = -1 ) {
        // Check if a team is selected and add it to the args.
        if ( -1 != $selected_team && ( 'sp_team' === get_post_type( $selected_team ) ) ) :
            $args['meta_query'][] = array(
                'key'     => 'sp_team',
                'value'   => array( $selected_team ),
                'compare' => 'IN',
            );
    
            // Only apply date filter for mid-season transfer rows (non-integer season_id)
            if ( (int) $season_id != $season_id ) {
                $date_from = false;
                foreach ( $data as $index => $season_data ) {
                    if ( (int) floor( $index ) != (int) $season_id ) {
                        continue;
                    }
                    $df = sp_array_value( $season_data, 'date_from', false );
                    if ( $df ) {
                        $date_from = $df;
                        break;
                    }
                }
                if ( $date_from ) {
                    $args['date_query'] = array(
                        array(
                            'after'     => $date_from,
                            'inclusive' => true,
                        )
                    );
                }
            }
    
            return $args;
        endif;
    
        // Limit data to same season
        foreach ( $data as $index => $season_data ) {
            if ( (int) $index == (int) $season_id ) continue;
            unset( $data[ $index ] );
        }
    
        // Sort the data by date
        uasort( $data, array( $this, 'sort_by_date' ) );
    
        // Move the internal pointer to the currently selected season
        while ( key( $data ) != $season_id ) next( $data );
    
        // Check if there is a data_from value and assign it to $date_from variable
        $date_from = sp_array_value( current( $data ), 'date_from', false );
    
        // Move pointer to next season
        next( $data );
    
        // Check if there is a following entry of same season and assign the date_from to $date_to variable
        $date_to = sp_array_value( current( $data ), 'date_from', false );
    
        if ( $date_from && $date_to ):
            $args['date_query'] = array(
                array(
                    'after'     => $date_from,
                    'before'    => $date_to,
                    'inclusive' => true,
                )
            );
        elseif ( $date_from ):
            $args['date_query'] = array(
                array(
                    'after'     => $date_from,
                    'inclusive' => true,
                )
            );
        elseif ( $date_to ):
            $args['date_query'] = array(
                array(
                    'before'    => $date_to,
                    'inclusive' => false,
                )
            );
        endif;
    
        return $args;
    }

    Tested on: SportsPress Pro 2.7.29, WordPress 6.8.1, baseball/softball configuration with mid-season transfers enabled.

    Hope this helps someone. Would be great if ThemeBoy could include this fix in the next release!

    — Reported and fixed by a community member.

    I think I can help you but I have to see something in the administrator to be correct. Are you willing to create a temporary user?

    Thread Starter granedoy

    (@granedoy)

    Thank you all for your responses. I decided to create a new league and put semi-final in the title. I then had to add each player to that new league for the stats to display.

    granedoy

    (@granedoy)

    O también puedes crear otra season con el mismo nombre y agregandole que es un playoff y como quien dice todo estará desde 0

    granedoy

    (@granedoy)

    Se me ocurre que hagas una tabla de manera manual con solo los datos de la serie regular.

    Glad you were able to solve it.

    Thread Starter granedoy

    (@granedoy)

    but that’s the standings. I am referring to the collective statistics per team. The plugin already does the individual ones but I am referring to the sum of everything per team

    Thread Starter granedoy

    (@granedoy)

    No, how could I do it? Could you be more specific?

    Try this to see if it works for you. In the list of players, assign a number to the order of the post in alphabetical order as you wish. I think in theory when you filter bat appearances, it should be placed how you want.

Viewing 9 replies - 1 through 9 (of 9 total)