Skip Navigation

[Resolved] Secondary Sorting by Custom Field

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

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

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 33 replies, has 2 voices.

Last updated by keithT-3 6 years ago.

Assisted by: Shane.

Author
Posts
#623046
Screen Shot 2018-03-07 at 10.37.28 AM.png

Tell us what you are trying to do?
I want/need to have both the "Order By" and the "Secondary Sorting" be done by custom fields. I see currently that the Secondary Sorting does not allow that to be done via custom fields. Is there a workaround to this limitation?

Is there any documentation that you are following? na

Is there a similar example that we can see? na

What is the link to your site?
If I can implement sorting by two custom fields it will be implemented on this page - hidden link. Currently the view is ordered by the custom field acf_shipping_address. But I need to have be the secondary sort, and a primary sort by acf_directory_sort_order. These are both Advanced Custom Fields, not Types Custom Fields, if that matter.

#623078

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

Thank you for contacting our support forum.

Natively we are not able to do this with views, however with a custom hook its made possible.
https://toolset.com/forums/topic/custom-order-by-with-2-custom-fields/#post-387775

The link above serves as an example code that was used to achieve this for a customer.

Please let me know if this helps.
Thanks,
Shane

#623126

The code below comes close

// iDS: added for secondary sort on Views
add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
        if ( $view_id != 16257 ){
            return;
        }
 
       $meta_query = array();
       $meta_query[] = array('key' => 'acf_directory_sort_order');
       $meta_query[] = array('key' => 'acf_shipping_city');
       if ( !isset($query_args['meta_query']) ){
            $query_args['meta_query'] = array();
       }
       $query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
       add_filter('posts_orderby', 'custom_order');
 
   return $query_args;
}
function custom_order($orderby) {
   global $wpdb;
   return $wpdb->postmeta.'.meta_value, mt1.meta_value, post_date ASC';
}

But it doesn't seem to be doing the sorting by the sort order as I wanted. Still seems to be sorting by acf_shipping_city. Question, do I have the call of the ACF correct?

FYI, I have to go and add values to the new ACF before I can implement this fully on the website, otherwise I'd direct you to a page where you can see. I will know it is working when the top two listings on this page go to the bottom - hidden link. They should go to the bottom because I have their "Directory Sort Order" set to 200 as the custom value, and the other listings on the page have a value of 100.

#623693

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

In order for this to work we need to know exactly the reference name of the acf custom field on the backend.

Could you contact the support team for ACF and ask them to show you how the custom field name is store on the backend because the hook is searching for the exact field name.

Thanks,
Shane

#623760
Screen Shot 2018-03-09 at 7.51.51 AM.png
Screen Shot 2018-03-09 at 7.47.10 AM.png
Screen Shot 2018-03-09 at 7.45.19 AM.png

Would that be the "meta_key" as it is stored in the database?

There are these meta_keys in the database
_acf_directory_sort_order (all these rows have a meta_value = field_5aa02dee37b1b)
_acf_shipping_city (all these rows have a meta_value = field_5994b3ee85871)
acf_directory_sort_order (these has real values in meta_value)
acf_shipping_city (these has real values in meta_value)

See attachments if they help. I would think the reference name has to be one of the above.

#623817

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

I would suspect that acf_directory_sort_order would work.

Would you mind allowing me to have access to the website so that I can see what the view debug information is returning ?

The private fields will be enabled for your next response.

Thanks,
Shane

#624635

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

I want to make some changes to the code but the ftp login isn't working.

This line here:

        if ( $view_id != 16257 ){

Should be

        if ( $view_id == 16257 ){

Please try this and let me know the results.

Thanks,
Shane

#625109

My apology, Shane, for not responding sooner. I didn't get an email notification of your response and just now had time go in and check. Also, I updated the FTP info and verified it works. Please try again. Don't worry about the "folder" value I put in there, once you log in you'll be in the folder for the dev site automatically.

I did try your change and it showed NO results on this page - hidden link.

Also, when we do get it working, I also want the same two options on view ID #1017. That view isn't fully populating right now, but that has to do with some configuration on my end - hidden link

#626299

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

I must apologize for the delay in response as i'm not able to fully debug this as yet for you.

Please be patient with me as i'm trying to get to it as soon as possible.

Thanks,
Shane

#626323

Thanks for the update. Let me know when you've figured this one out!

#626751

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

It seems the ftp is not retrieving the directory list.

The server keeps timing out. However I do see that the code is affecting the view here hidden link

I think its better I took a duplicator package and work on this one locally to see how best I can get this up and running.

Thanks,
Shane

#626768

Bummer. I'm able to log in via FTP using Transmit here on my Mac and am not getting any errors using your credentials.

Let me know what I can do to help, as it would be very nice to get this resolved soon. I appreciate your patience and perseverance.

Keith

#627302

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

I'm currently creating the duplicator package for this site.

I also noticed that there is a difference in the pages here.
hidden link

And
hidden link

I need to know exactly which view I should look at. Once I get it and confirm it working for that view then I can explain what i did.
Sorry for this taking so long.
Thanks,
Shane

#627347

Thanks for asking. I just went in and looked at both. The View on each looks identical.

hidden link is a copy of the other site. The only difference is that version/copy has the following code added to the functions.php file in the child theme

// iDS: added for secondary sort on Views
add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
        if ( $view_id == 16257 ){
            return;
        }
 
       $meta_query = array();
       $meta_query[] = array('key' => 'acf_directory_sort_order');
       $meta_query[] = array('key' => 'acf_shipping_city');
       if ( !isset($query_args['meta_query']) ){
            $query_args['meta_query'] = array();
       }
       $query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
       add_filter('posts_orderby', 'custom_order');
 
   return $query_args;
}
function custom_order($orderby) {
   global $wpdb;
   return $wpdb->postmeta.'.meta_value, mt1.meta_value, post_date ASC';
}

Once I remove that code then results load onto this page the same as on the live site:
hidden link

I have left that code off the functions.php file for the time being, so you can see the page load. As a result, that page only sorts by 'acf_shipping_city'. I need it to ASC sort by 'acf_directory_sort_order' first, and then by city.

#627878

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Keith,

I managed to fix the code.

Please try this and let me know if it helps.

add_filter('wpv_filter_query', 'add_custom_fields_to_view_query', 99, 3);
function add_custom_fields_to_view_query($query_args, $view_settings, $view_id ) {
        if ( $view_id == 16257 ){
        
 
       $meta_query = array();
       $meta_query[] = array('key' => 'wpcf-sort-priority');
       $meta_query[] = array('key' => 'acf_shipping_city');
       if ( !isset($query_args['meta_query']) ){
            $query_args['meta_query'] = array();
       }
       $query_args['meta_query'] = array_merge($meta_query, $query_args['meta_query']);
       add_filter('posts_orderby', 'custom_order');
   }
 
   return $query_args;
}
function custom_order($orderby) {
   global $wpdb;
   return $wpdb->postmeta.'.meta_value, mt1.meta_value, post_date ASC';
}

I see you have a sort order custom field so I used that instead of the acf sort order key that you had because I believe this was something internal that ACF uses.

Thanks,
Shane

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.