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!
pre_get_posts
is for posts andWP_Query
, butusers.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 wantpre_get_users
?