Custom default filtering in the user admin panel disables the other sortable columns

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?

Read more here:: Custom default filtering in the user admin panel disables the other sortable columns

Leave a Reply

Your email address will not be published. Required fields are marked *