I am in the process of adding an additional column to the Users admin screen to display the company for each user. I have been successful in getting this column to show in the table, but I am really struggling with making it sortable alphabetically.
The column header does seem to be activated as sortable, but if i click it to reorganise the list order, the table actually rearranges alphabetically based on the username as opposed to the company.
I have spent alot of time on the web looking for and adapting other solutions, but still no luck. I have seen plenty of examples of making custom columns sortable for post type admins screens but not the user admin screen.
Below is the code I am currently using to generate a "Company" column on the users admin screen and it is pulling in the author meta data "company".
//MAKE THE COLUMN SORTABLE function user_sortable_columns( $columns ) { $columns['company'] = 'Company'; return $columns; } add_filter( 'manage_users_sortable_columns', 'user_sortable_columns' ); add_action( "pre_get_users", function ( $WP_User_Query ) { if ( isset( $WP_User_Query->query_vars["orderby"] ) && ( "company" === $WP_User_Query->query_vars["orderby"] ) ) { $WP_User_Query->query_vars["meta_key"] = "company_name"; $WP_User_Query->query_vars["orderby"] = "meta_value"; } }, 10, 1 );
SQL JOINSthat are in that answer, for example.