0

I am attempting to implement some custom sorting logic for a column but are facing issues in the pre_get_posts hook. The value of the orderby property on the query is not returning the value corresponding to the currently selected sorted column. I have no plugins installed and are using the latest wordpress version 6.1.1 for context. Here is my code:

add_action( 'pre_get_posts', 'users_custom_column_query' );

function users_custom_column_query( $query ) {
    write_log( 'is main query: ' . var_export( $query->is_main_query(), true ) );
    write_log( 'orderby value: ' . var_export( $query->get( 'orderby' ), true ) );
}

Which outputs this on my local test site:

// Output on page site.local/wp-admin/users.php?orderby=email&order=asc:
// is main query: true
// orderby value: ''

Expected output is 'email'. Any help is greatly appreciated!

1
  • pre_get_posts is for posts and WP_Query, but users.php lists users not posts. Lots of plugins/themes etc use queries too, pre_get_posts can be called multiple times on a request. Are you sure you didn't want pre_get_users?
    – Tom J Nowell
    Dec 27, 2022 at 16:23

1 Answer 1

1

As Tom J Nowell pointed out in the comments, I was targeting the wrong hook for this. Correct hook to modify the WP_User_Query is pre_get_users.

Your Answer

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

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