Skip Navigation

[Resolved] Sorting users by 2 Custom Fields

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

Problem:
How can we sort a User View by more than one Custom Field?

Solution:
It requires custom code that applies the wpv_filter_user_query() filter.
You can set a secondary sorting option following the WP_User_Query rules.
Full stack example in below doc.

Relevant Documentation:
https://toolset.com/forums/topic/expanding-sort-function-users/#post-1257953
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query
http://codex.wordpress.org/Class_Reference/WP_User_Query#Parameters

This support ticket is created 5 years, 7 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by pascalH 5 years, 7 months ago.

Assisted by: Beda.

Author
Posts
#1255637
Sort user.JPG

Good day

I am currently working on a view for showing users. a sorting by company name has already been made.
I would also like to sort the companies (users) by region (provincie). The intention is that the users are sorted by region (=provincie) and by company name (=bedrijfsnaam) (in alphabetic order).
Both fields are custom user fields.

Are there possibilities to get this done?

Greetings and thank you
Pascal

#1255669

Not natively, because Toolset User Views do not support secondary Ordering and Post Views which support it, are not the adequate tool here and on top do not support Custom Fields in the secondary Ordering.

You'd have to hook into the wpv_filter_user_query, and alter the orderby with WP User Query arguments.
It's explained here, and below is an example:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query
http://codex.wordpress.org/Class_Reference/WP_User_Query#Parameters

add_filter( 'wpv_filter_user_query', 'prefix_unset_user_roles', 99 );
 
function prefix_unset_user_roles( $query_args ) {
	$query_args = array(
    'orderby' => array(
        'wpcf_test_user' => 'DESC',
        'wpcf_another_field' => 'DESC',
    ),
    'meta_query' => array(
        'relation' => 'AND',
        'wpcf_test_user' => array(
            'key' => 'wpcf-test-user',
        ),
        'wpcf_another_field' => array(
            'key' => 'wpcf-another-field',
        )
    )
);
    //var_dump($query_args['orderby']);
    return $query_args;
}
#1256085
Sort-province.JPG

Hello Beda

Thanks for the reply, it is the first time that we have come into contact with each other.
I have read some interesting tips from you, nice to meet you!
I've tried but I can't get the job done. I think I understand the explanation not well, I find it very difficult.

What I have done in the meantime:
I changed the sorting function in the toolset settings to Provincie (=region see image) (wpcf-provincie-bedrijf)
in this way the users are displayed by region in alphabetical order.
Only the subscribers are shown and that is the intention.

What needs to be done is that the company names of these subscribers are also in alphabetical order (wpcf-bedrijfsnaam)
The users (subscribers) must be shown by region (wpcf-provincie-bedrijf) in alphabetical order and company name (wpcf-bedrijfsnaam) in alphabetical order. There must be no limit for showing users (subsribers)

I used your sample code to create the sort function, I played with the code and added the code to functions.php and looked at the result. The results: 1) no changes to be seen, 2) the changes are seen but now all users are shown, not only subscribers.

Through the url https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query I saw that a fiter can be set, do I have to add these separately to the code you gave me? (see below).
Can you indicate to me how this should be completed?

//If you have a filter in the View to include users from different roles and do not want the role set in the Content selection section to be applied, you can do something like this
add_filter( 'wpv_filter_user_query', 'prefix_unset_user_roles' );

function prefix_unset_user_roles( $query_args ) {
if ( isset( $query_args['role'] ) ) {
unset( $query_args['role'] );
}
return $query_args;
}

As for the example code below, what exactly must be entered at wpcf_test_user?
I entered there in the code wpcf-provincie-bedrijf, at wpcf_another_field I added wpcf-bedrijfsnaam

add_filter( 'wpv_filter_user_query', 'prefix_unset_user_roles', 99 );

function prefix_unset_user_roles( $query_args ) {
$query_args = array(
'orderby' => array(
'wpcf_test_user' => 'DESC',
'wpcf_another_field' => 'DESC',
),
'meta_query' => array(
'relation' => 'AND',
'wpcf_test_user' => array(
'key' => 'wpcf-test-user',
),
'wpcf_another_field' => array(
'key' => 'wpcf-another-field',
)
)
);
//var_dump($query_args['orderby']);
return $query_args;
}

I have tried various combinations, but can't really get it done.
Would you still help me with this?

Thanks in advance and enjoy the weekend!
Greetings Pascal

#1257953

Yes - I made a mistake in the code, with my sample you'll overwrite the entire query, but you should only overwrite the ordering, hence $query_args['orderby'] and not $query_args.

To apply it, you'd order in the View's settings by one whatever you wish, since you'll overwrite it anyway with the code.
The Code then is added to the Theme or Types Custom Code - note that below is an example - it needs to be adapted to your site and can be used only as an example/reference:

add_filter( 'wpv_filter_user_query', 'prefix_unset_user_roles', 99 );
  
function prefix_unset_user_roles( $query_args ) {

    $query_args['orderby'] = array( //2 new keys to orderby
        'wpcf_custom_field_one' => 'DESC',//you can choose any name you want here, to replace wpcf_custom_field_one or wpcf_custom_field_two
        'wpcf_custom_field_two' => 'ASC',//you can determin the ASC and DESC for each orderby option created here
    );
    $query_args['meta_query'] = array(
        'relation' => 'AND',
        'wpcf_custom_field_one' => array(
            'key' => 'wpcf-singleline',//here you'll need to add your real Custom Field slug, prefixed with a wpcf-
        ),
        'wpcf_custom_field_two' => array(//here you'll need to add your real custom field. 
            'key' => 'wpcf-single',
        )
    );
    return $query_args;
}

Note that for Custom Code solutions that go above examples, you might want to consult the Contractors as well (https://toolset.com/contractors/) in case you need help with more customizations.

#1261633

Hello Beda

With your help it worked!
Thank you a lot.

Greetings Pascal