Skip Navigation

[Resolved] Sorting users by 2 Custom Fields did not succeed

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
- 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/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by Waqar 5 years, 6 months ago.

Assisted by: Waqar.

Author
Posts
#1263899
example-order.jpg

Good day

A few days ago I created a ticket.
It concerns ticket https://toolset.com/forums/topic/expanding-sort-function-users/

it concerns the following: I made a view, this view shows all users (subscribers).
The following is displayed: region, company name and address.

I would like all users to be sorted automatically by region and company name, the challenge is that the sorting is in alphabetical order (a-z). this means that the region is in alphabetical order but also the company names in alphabetical order (a-z).

Good to know, it concerns the following field slugs:
wpcf-provincie-bedrijf (=region)
wpcf-bedrijfsnaam (=company name)

I created the code below based on my previous ticket and added it to functions.php

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-provincie-bedrijf',//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-bedrijfsnaam',
)
);
return $query_args;
}

I have modified the code in different ways and tried everything, but it still doesn't work.
At first I thought it worked, but after extensive testing with multiple users and regions, this is not the case.

Awaiting your reply and many thanks in advance!
Greetings Pascal

#1265081

Hi Pascal,

Thank you for contacting us and sorry to learn that the suggested code is not working as expected for you.

To troubleshoot this in more detail, I'll need your website's clone/duplicator package.

Here is a guide on how to share the website's clone/duplicator package:
https://toolset.com/faq/provide-supporters-copy-site/

Note: your next reply will be private so that you can share it securely.

regards,
Waqar

#1267695

Hi Pascal,

Thanks for writing back and for sharing these details.

I've checked the page that you referred to and noticed that the custom function to order the results, based on two custom fields is working as expected.
( screenshot: removed )

The same can be confirmed on a new example page, with better visibility:
( screenshot: removed )

Based on what you've shared in your last message, you don't just need to order the results, but in fact, you need to group them together too, based on the "Regions".
( this is different from what was discussed in the other ticket: https://toolset.com/forums/topic/expanding-sort-function-users/ )

To achieve what you're planning, you'll need two views:

1. A parent view to show "Example view to show all user regions":

This view will need to be set to show a list of users, ordered by the "Region" user field )

2. A child view to show "Example view to show user companies based on a region":

This nested view will also show the list of users, but it will have a query filter to show only the users, which have the "Province" field value same as the one passed as the shortcode attribute from the parent view.
( ref: https://toolset.com/documentation/user-guides/passing-arguments-to-views/ )

Important notes:

a). The code snippet to use "wpv_filter_user_query" has been commented out since it is not needed.

b). To control the order of the Region names, you can arrange them in the "Provincie" field's settings, since it is a "Select" type field.
( screenshot: removed )

3. Note that a custom script will be needed to remove the duplicate Region entries from the parent view "Example view to show all user regions"
( screenshot: removed )


jQuery(document).ready(function(){
  var seenGenre = {};
  // loop over regions
  jQuery('.provincie-wrap').each(function(index,item) {
    var txt = jQuery(item).text();
    
    var $h3 = jQuery(item).closest('h3');
    
    // if region has been seen, remove it
    if (seenGenre[txt] || seenGenre[txt + ', ']){
        jQuery(item).remove();
    }else{
        seenGenre[txt] = true;
    }
  });
});

I hope this helps and for more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1268433

Hello Waqar

Thanks for the quick reply

I have to take a good look at it myself.
I will come back to you after the weekend with my findings.
Please leave the ticket open until further notice

Thanks for the assistance!
Greetings Pascal

#1269091

You're very welcome Pascal and I'll wait to hear back from you.

Have a great weekend!

regards,
Waqar

#1271101

Hello Waqar

Thank you for this explanation,
with the help of the explanation it has indeed been possible to solve the challenge.

Thanks a lot

Would you please hide the links and screenshots that you sent me on June 13, 2019 at 9:22 am?
We would like to keep this private.

Thanks for your great support!
Greetings Pascal

#1271285

Hi Pascal,

Thanks for the update and glad that my message helped.

I've removed the site-specific links and screenshots from my last reply ( https://toolset.com/forums/topic/sorting-users-by-2-custom-fields-did-not-succeed/#post-1267695 ) and have included some generic description so that it can still help others, with some context.

regards,
Waqar