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, 8 months 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.
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.
// 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.
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.
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.
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
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
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.
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.