1

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 ?

1 Answer 1

3

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' );
3
  • You can probably change the if condition to if( !isset($_GET['orderby']) ) {. I didn't confirm whether pre_get_users is called anywhere other than admin.
    – BillK
    Commented Feb 7, 2016 at 9:15
  • 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
  • Nice! Now how would you add descending order to that function, so that the most recent users appear on top?
    – fdmsaraiva
    Commented Aug 30, 2022 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.