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 ?

2

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' );
  • 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 Feb 7 '16 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. – hkchakladar Feb 7 '16 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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