My setup
I've got a question on the filtering on the user filter in the admin panel. I've added the code to display the "registration_date" filter, which is working fine. I found this website to describe the process. This part works fine.
Now, I'm trying to implement a default ordering by most recent user registred. In my function.php, I've tried the approach which I've found here. This part works partially only. Here is my code :
add_action('pre_get_users', 'change_user_order');
function change_user_order($query) {
$screen = get_current_screen();
if( is_admin() && 'users' == $screen->base) {
if( !isset($query->query['orderby']) ) {
$query->set('orderby', 'registered');
$query->set('order', 'DESC');
}
}
// We need to remember to return the altered query.
return $query;
}
My Issue
By default, this part works fine as the filtering is done by the most recent. Now it also seems that it desactivates all other possibility to sort other sortable columns (ex. asc login). for example, this url wp-admin/users.php?orderby=login&order=asc
would not show the user ordered by login field. What did I do wrong?
Update - Accepted solution & final code
Thanks to Sally, I was able to change my initial code so that my initial requirements are all fullfilled.
For the sake of completness, the final code for this default selection of sortable and order columns in the user panel is attached here :
add_action('pre_get_users', 'change_user_order');
function change_user_order($query) {
$screen = get_current_screen();
if( is_admin() && 'users' == $screen->base) {
// Set default `orderby` to 'registered'.
if( empty( $_REQUEST['orderby'] ) || !isset($query->query_vars['orderby']) ) {
$query->set('orderby', 'registered');
}
// Set default `order` to 'DESC'.
if( empty( $_REQUEST['order'] ) || !isset($query->query_vars['order']) ) {
$query->set('order', 'DESC');
}
}
// We need to remember to return the altered query.
return $query;
}