I want to sort users by User ID on users.php
by default. The page is currently ordered by username
as default.
Is there any hook to alter the order_by
option on users.php
?
You can use pre_get_users
since WP 4.0.0
function my_user_sort( $query_args ){
if( is_admin() && !isset($_GET['orderby']) ) {
$query_args->query_vars['orderby'] = 'ID';
}
return $query_args;
}
add_action( 'pre_get_users', 'my_user_sort' );
if( !isset($_GET['orderby']) ) {
. I didn't confirm whether pre_get_users
is called anywhere other than admin.
if( is_admin() && !isset($_GET['orderby']) )
condition is fine for me, since I only need this on admin. I think this is called anywhere, when the users query is called. Thanks for the answer.
Commented
Feb 7, 2016 at 20:10