1

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;
}

1 Answer 1

0

First, use $query->query_vars and not $query->query.

Secondly, you should run this conditional check before overriding the corresponding query var:

empty( $_REQUEST['{VAR}'] ) || !isset($query->query_vars['{VAR}'])

where {VAR} corresponds to the query var's name/key such as orderby.

So try this, which worked for me:

// 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');
}

which you would use in place of:

if( !isset($query->query['orderby']) ) {
    $query->set('orderby', 'registered');
    $query->set('order', 'DESC');
}
4
  • Many thanks for your help, indeed, this looks more adequate than what I tried so far. I'll try this right now
    – XaWin
    Commented Aug 19, 2018 at 9:37
  • Sure, take your time. And hopefully it works for you.
    – Sally CJ
    Commented Aug 19, 2018 at 9:49
  • 1
    And it worked fantastically, thanks a lot, I'll accept your answer now!
    – XaWin
    Commented Aug 19, 2018 at 9:49
  • You're very welcome. =)
    – Sally CJ
    Commented Aug 19, 2018 at 9:53

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.