Skip Navigation

[Resolved] Display users in order of a custom user field

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

Problem:
How to order a Users View by a custom user field

Solution:
Changing the order by option to a non-standard setting not included in the GUI dropdown requires a custom code snippet using the wpv_filter_user_query filter:

add_filter( 'wpv_filter_user_query', 'custom_orderby_func', 100, 3 );
 
function custom_orderby_func( $query_args, $view_settings, $view_id ) {
    if ( $view_id == 123 ) {
        $query_args['meta_key'] = 'wpcf-company';
        $query_args['orderby'] = 'meta_value';
        $query_args['order'] = 'ASC';
    }
    return $query_args;
}

Change the meta_key as required.

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_user_query/

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.

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by gaborG 5 years, 4 months ago.

Assigned support staff: Nigel.

Author
Posts
#402056
Order Users.jpg

Is it possible to order users based on a custom field what I added to them?

I added a "Company" field to them, which refers to the company for which they work. And I want to order them based on company name from A to Z, because that's how they can be easily found. But I don't see this option. I don't see any custom field options to order them by.

If it's not possible, then I would like to request that you make it possible. 🙂

#402167

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Non-standard User fields are not available in the Order by select box.

You can override the orderby setting using a small snippet of custom code that you can add to your theme's functions.php file.

You are going to hook into the wpv_filter_user_query filter, as described here:
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_user_query/

You are going to need to replace the id of the View you want this override to apply to in the code below, and I have assumed the slug of your company custom field is "company".

add_filter( 'wpv_filter_user_query', 'custom_orderby_func', 100, 3 );

function custom_orderby_func( $query_args, $view_settings, $view_id ) {
    if ( $view_id == 123 ) {
        $query_args['meta_key'] = 'wpcf-company';
        $query_args['orderby'] = 'meta_value';
        $query_args['order'] = 'ASC';
    }
    return $query_args;
}

Let me know if you have any problems getting this to work.

#402358

Something's not right.
I took the View's ID from the URL (...admin.php?page=views-editor&view_id=1399) and changed the 123 to 1399. I actually use "companyname" as the custom field name (I used "company" before for a custom field what I no longer use but your plugin doesn't release these names), so I changed the name to "companyname".
Then it doesn't display any results. It says no results, even though it should have 3.

What can be the problem?

#402626

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Sorry, simplest of typos, I had a double underscore in my function name.

I will edit the previous reply to fix to just a single underscore.

Apologies if it had you scratching your head longer than it had me scratching mine.

#402629

Alright, that's it. Now it works perfectly, ordering by company name.
Thank you Nigel. 🙂