Filter by custom user meta
<?php
add_action('pre_user_query', 'my_user_query');
function my_user_query($userquery){
if(!is_admin())
return;
// if('wallet'==$userquery->query_vars['orderby']) {//check if church is the column being sorted
// global $wpdb;
// $userquery->query_from .= " JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) ";//note use of alias
// $userquery->query_where .= " AND alias.meta_key = 'wallet' ";//which meta are we sorting with?
// $userquery->query_orderby = " ORDER BY CAST(alias.meta_value AS UNSIGNED) ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc " ) ;//set sort order
//
// //print_pre($userquery);
// }
}
if( is_admin()){
add_action('pre_get_users', function($query){
$orderby = $query->query_vars["orderby"];
$meta_query = array(
'relation' => 'OR',
array(
'key' => $orderby,
'compare' => 'NOT EXISTS'
),
array(
'relation' => 'OR',
array(
'key' => $orderby,
'value' => 1
),
array(
'key' => $orderby,
'value' => 1,
'compare' => '!='
)
)
);
$query->set('meta_query', $meta_query);
$order = $query->query_vars["order"] == "asc" ? "ASC" : "DESC";//set sort order
$query->set('orderby', array('meta_value_num' => $order, 'date' => $order));
},999);
}
?>