To avoid a second query just for sorting you could use the pre_user_query
hook to support a custom orderby
value.
This code will enable sorting users by reputation in a single query just by setting orderby
to reputation
.
function wpse_296999_pre_user_query( $user_query ) {
global $wpdb;
$order = $user_query->query_vars['order'];
$orderby = $user_query->query_vars['orderby'];
// If orderby is 'reputation'.
if ( 'reputation' === $orderby ) {
// Join reputation table.
$user_search->query_from .= " INNER JOIN {$wpdb->prefix}user_reputation AS reputation ON {$wpdb->users}.ID = reputation.userid";
// And order by it.
$user_search->query_orderby = " ORDER BY reputation.user_reputation_score $order";
}
}
add_action('pre_user_query','wpse_27518_pre_user_query');
Just make sure that on this line:
if ( 'reputation' === $orderby ) {
'reputation'
is whatever the actual name of your sortable column is.