Skip to content

Instantly share code, notes, and snippets.

@kadimi
Last active August 2, 2025 16:12
Show Gist options
  • Select an option

  • Save kadimi/e846e3bbb0606de819ecebd6cc2e59b0 to your computer and use it in GitHub Desktop.

Select an option

Save kadimi/e846e3bbb0606de819ecebd6cc2e59b0 to your computer and use it in GitHub Desktop.
SportsPress: Sort Players in Event Performance (supports multiple sorting callbacks)
<?php
add_action('init', function () {
$sort_by_status_cb = function ($a, $b) {
return ($a['status'] === 'sub') <=> ($b['status'] === 'sub');
};
$sort_by_position_cb = function ($a, $b) {
$get_positions_by_order = function () {
$positions = get_terms(array(
'taxonomy' => 'sp_position',
'hide_empty' => false,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => 'sp_order',
));
return wp_list_pluck($positions, 'name', 'term_id');
};
$ordered_positions = array_keys($get_positions_by_order());
$get_min_index = function ($positions) use ($ordered_positions) {
$positions = (array) $positions;
foreach ($ordered_positions as $index => $term_id) {
if (in_array($term_id, $positions)) {
return $index;
}
}
};
return $get_min_index($a['position']) <=> $get_min_index($b['position']);
};
$composite_sort_cb = fn(...$cbs) => fn($a, $b) => array_reduce($cbs, fn($r, $cb) => $r ?: $cb($a, $b), 0);
add_filter('sportspress_event_performance_players', function ($data) use (
$composite_sort_cb,
$sort_by_position_cb,
$sort_by_status_cb,
) {
if (empty($positions_ids)) {
return $data;
}
$headers = $data[0];
$sorted_data = array_slice($data, 1, null, true);
uasort($sorted_data, $composite_sort_cb(
$sort_by_status_cb,
$sort_by_position_cb,
));
$data = array_merge(array($headers), $sorted_data);
return $sorted_data;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment