Skip Navigation

[Resolved] User view not displaying "Last Name" in "Ordering"

This support ticket is created 7 years, 1 month 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by marcB-6 7 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#577362
Screenshot_37.png

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.

#577542
lastname.JPG

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

#577546

I have added above code but it doesn't seems to be working here : hidden link 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 : hidden link

If I remove filter from functions.php file it is showing users here : hidden link

Thanks

#577551

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

#577563

Thanks for the details, I assume the problem page is this one:
hidden link

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

#577574

Thanks