I am trying to display users order by "Last Name".
I have created view to display users.
I expected to see "Last Name" in "Ordering" dropdown in view.
In "Ordering" dropdown I can not see "Last Name" field.See screenshot
Thanks.
Dear Marc,
Thanks for the feedback, as you can see, there isn't such an option to order the user view by user's last name field, but you can override the "orderby" settings with some custom codes by using filter hook "wpv_filter_user_query".
For example, you can try this:
1) edit the user view, in section "Query Filter", add a filter:
Select items with field:
last_name is a string different from
see screenshot lastname.JPG
2) Add below codes into your theme/functions.php:
add_filter('wpv_filter_user_query', 'my_func1', 999, 3);
function my_func1($query_args, $view_settings, $view_id){
if($view_id != 123)return $query_args;
$query_args['orderby'] = 'meta_value';
$query_args['order'] = 'ASC';
return $query_args;
}
Please replace 123 with your user view's ID, and test again.
More help:
wpv_filter_user_query
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query
Order & Orderby Parameters
https://codex.wordpress.org/Class_Reference/WP_User_Query#Order_.26_Orderby_Parameters
I have added above code but it doesn't seems to be working here : enlace oculto look at "Delegates" section. Users are not sorted by last name.
Also, the filter you gave is creating problem with my other view, In that it is not showing user at all here : enlace oculto
If I remove filter from functions.php file it is showing users here : enlace oculto
Thanks
I have already modified above codes, and tested it in my localhost, it works fine, and it is only an example, you will need to customize it according to your website, if you still need assistance for it, please provide a test site with same problem, and fill below private detail box with login details and ftp access, also point out where I can edit your PHP codes, I need a live website to test and debug it, thanks
Thanks for the details, I assume the problem page is this one:
enlace oculto
And I have modified the PHP codes as below, please test again, check if it is fixed.
add_filter('wpv_filter_user_query', 'my_func1', 999, 3);
function my_func1($query_args, $view_settings, $view_id){
if($view_id != 489)return $query_args;
$query_args['orderby'] = 'meta_value';
$query_args['order'] = 'ASC';
$query_args['meta_key'] = 'last_name';
return $query_args;
}
More help:
https://codex.wordpress.org/Class_Reference/WP_User_Query#Order_.26_Orderby_Parameters
'meta_value' - Note that a 'meta_key=keyname' must also be present in the query