Title: Sorting Bug
Last modified: August 11, 2026

---

# Sorting Bug

 *  [pittdsl](https://wordpress.org/support/users/pittdsl/)
 * (@pittdsl)
 * [2 weeks, 3 days ago](https://wordpress.org/support/topic/sorting-bug-4/)
 * Hi Savvas, First thank you for the wonderful plug in, it has solved a display
   issue for how our soccer league works and has made my life a lot easier. In preparation
   for the new season, I’ve updated WordPress (7.0.3) and all the plug ins and I’m
   seeing an issue. Here’s the table:
 * ![](https://i0.wp.com/pittdsl.org/tournament/wp-content/uploads/2026/08/Table.
   png)
 * And here’s the sorting criteria summary
 * ![](https://i0.wp.com/pittdsl.org/tournament/wp-content/uploads/2026/08/Sorting.
   png)
 * Team points is the only Regular order and everything else is Tiebreak order. 
   The two teams in first place didn’t play so the H2H Team points shouldn’t matter.
 * I threw Claude at it and it wrote up a bug report…
 * **Example:** Two teams tied on 6 points, 0 games played against each other. Team
   A has GD +2, Team B has GD +3. Expected order: Team B, then Team A. Actual order:
   Team A, then Team B — i.e., alphabetical, ignoring GD entirely.
 * **Root cause:**
 * In `includes/class-sah2h-league-table.php`, `data()` recomputes standings for
   a tied group by re-querying events scoped only to those specific teams (effectively
   head-to-head-only). For any Tiebreak Order column, the code is supposed to restore
   the full-season value when the column is _not_ marked “H2H Only”:
 *     ```wp-block-code
       // ~line 792
       foreach ( $this->h2h_priorities as $temp_priority ) {
           if ( isset( $temp_priority['h2h_only'] ) && '' == $temp_priority['h2h_only'] ) {
               // swap in full-season stat
               foreach ( $team_ids as $temp_team_id ) {
                   $merged[ $temp_team_id ][ $temp_priority['column'] ] = $this->temp_merged[ $temp_team_id ][ $temp_priority['column'] ];
               }
           }
       }
       ```
   
 * The `h2h_only` field comes from a plain `<input type="checkbox" name="sah2h_tiebreak_order[
   i][h2h_only]" value="1">` in `class-sah2h-tiebreak-criteria.php`. When that checkbox
   is left **unchecked** (the normal setting for a season-wide fallback stat like
   GD), standard HTML form behavior means the field isn’t submitted at all — so 
   after saving, `sah2h_tiebreak_order` has _no_ `h2h_only` key for that row, rather
   than an empty string.
 * Because of that, `isset( $temp_priority['h2h_only'] )` evaluates to `false` for
   any unchecked row, the `if` never runs, and the full-season swap-back never happens.
   The column then compares using only the (empty, in this case) head-to-head data
   between the tied teams — every team gets `0`, `h2h_sort()` in the same file sees
   no difference, and once all configured columns are exhausted it falls back to`
   strcmp()` on team name (alphabetical), regardless of the real season-wide GD.
 * **Suggested fix:** treat “key not set” the same as “empty string” when deciding
   whether to restore full-season data, e.g.:
 *     ```wp-block-code
       if ( '' == sp_array_value( $temp_priority, 'h2h_only', '' ) ) {
       ```
   
 * or normalize on save in `advanced-h2h-for-sportspress.php` so unchecked boxes
   are stored as `h2h_only => ''` rather than omitted.
 * **To reproduce:**
    1. Create a Sorting Criteria post with Advanced Sorting.
    2. Set Tiebreak Order: Points (DESC), then Goal Difference (DESC), leaving “H2H
       Only” **unchecked** on Goal Difference.
    3. Apply it to a league table where two teams are tied on points but have not played
       each other, and have different season GD.
    4. Observe the tied teams sort alphabetically instead of by GD.
 * I tried to make the change manually, but it doesn’t look to be working. That 
   said I’m not a a PHP/WordPress Dev so maybe I’m missing something. Not to mention
   Claude gets things wrong…
 * Thanks again for the amazing addition to SportsPress and let me know if you need
   any more information!
 * Bob

You must be [logged in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fsorting-bug-4%2F%3Foutput_format%3Dmd&locale=en_US)
to reply to this topic.

 * ![](https://ps.w.org/advanced-h2h-for-sportspress/assets/icon.svg?rev=2838705)
 * [Advanced H2H for SportsPress](https://wordpress.org/plugins/advanced-h2h-for-sportspress/)
 * [Support Threads](https://wordpress.org/support/plugin/advanced-h2h-for-sportspress/)
 * [Active Topics](https://wordpress.org/support/plugin/advanced-h2h-for-sportspress/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/advanced-h2h-for-sportspress/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/advanced-h2h-for-sportspress/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [pittdsl](https://wordpress.org/support/users/pittdsl/)
 * Last activity: [2 weeks, 3 days ago](https://wordpress.org/support/topic/sorting-bug-4/)
 * Status: not resolved