Sort column in Users admin Tab

  • ✅ You’ve added the column!
  • ✅ You’ve told the admin UI it’s a sortable column!
  • ❌ You didn’t implement the sorting of the user query.

It’s not enough to tell WP Admin that you have a sortable column, how is it meant to know how to sort it ¯\_(ツ)_/¯! It’s not that clever, it has no idea what your function that displays the content is displaying or what kind of HTML it’ll return it just calls it and trusts it’ll do the job. How do we know the output from add_listing_count_column_row isn’t a youtube video or a random number?

The sorting itself is done via the database, the PHP just creates the query and displays the results. It isn’t going to get the results of every user and sort them in PHP, that would limit the number of users in WordPress to a single page of results.

So instead, use the pre_get_users hook that same way you would use pre_get_posts, look for the URL get parameter it added to indicate the sorting and direction, double check is_admin is true so you don’t mess up the frontend, and modify the WP_User_Query to sort the users using the sorting/order query variables in the WP_User_Query documentation.

Note that this will lead you to a new and separate question though that you need to ask a brand new question: “How do I sort a WP_User_Query by a users specific post type count?”