Skip to content

Instantly share code, notes, and snippets.

@kadimi
Created November 12, 2023 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadimi/4543f0246ba1bea03eec4125809737a4 to your computer and use it in GitHub Desktop.
Save kadimi/4543f0246ba1bea03eec4125809737a4 to your computer and use it in GitHub Desktop.
SportsPress - Timeline Fix
<?php
/**
* Custom sorting function for timeline elements.
*
*
* ## The Problem
*
* When many timeline elements have the same minute(e.g, a goal and an assist
* both at 33'), they're thrown on the timeline in a random order are sorted.
* This custom sorting function fixes this problem. It sorts the elements based on
* the performance columns menu orders.
*
* ## How to use
*
* Override the timeline template(s) you're intereted in and add this code after
* the line that says `$timeline = $event->timeline( false, true );`.
*/
usort($timeline, function ($a, $b) {
if ($a['time'] != $b['time']) {
return $a['time'] > $b['time'];
}
static $performance = [];
$a_key = $a['key'];
$b_key = $b['key'];
if (empty($performance[$a_key])) {
$performance[$a_key] = get_posts([
'post_type' => 'sp_performance',
'name' => $a_key,
])[0];
}
if (empty($performance[$b_key])) {
$performance[$b_key] = get_posts([
'post_type' => 'sp_performance',
'name' => $b_key,
])[0];
}
return $performance[$a_key]->menu_order < $performance[$b_key]->menu_order;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment