Skip Navigation

[Resolved] Sort users view

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to sort a Users View by last name, and possibly show the results in a sortable table.

Solution: User Views use WordPress's WP_User_Query class to query users, and there is no option to order by last name. However, you can use the wpv_filter_user_query hook to pass sorting criteria into the View.

Relevant Documentation: https://toolset.com/forums/topic/sort-user-view-by-users-last-name-of-custom-field/
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_user_query/

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by romanB-3 7 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#560880

Hello,

I have a view I want to sort by user last name but last name doesn't appear as an option in the sorting options.

Also I have made the view with the wizard and checked "Make the table sortable by columns" but the sorting little arrows don't appear on this view.

Thank you.

#560911

Toolset uses the WP_User_Query function to query Users, and there is no option to query by last name. This is a limitation of WordPress: https://codex.wordpress.org/Class_Reference/WP_User_Query#Order_.26_Orderby_Parameters

There's another ticket in the forum that discusses how you can hard-code this sorting using the wpv_filter_user_query hook:
https://toolset.com/forums/topic/sort-user-view-by-users-last-name-of-custom-field/

Unfortunately there is not a good way to implement User sorting using a sortable table at this time. If you want to make it user-sortable, you must use custom code to add a custom sorting filter to your View, then capture the sort value and pass it into the wpv_filter_user_query hook:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query

#561186

Thank you very much, the snippet to sort by last name worked great.
User sorting in tables seems a bit complicated right now ; but that would be a great feature to implement.