为了避免仅仅为了排序而进行第二次查询,您可以使用pre_user_query
挂钩以支持自定义orderby
价值
此代码仅通过设置即可在单个查询中按信誉对用户进行排序orderby
到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\');
只需确保在这一行:
if ( \'reputation\' === $orderby ) {
\'reputation\'
是可排序列的实际名称。