Sorry, it does not work. I added your code as-is to the functions.php and it did not change the order. It still is sorting by the city only on this page - hidden link.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Keith,
This is because on most of the posts that I checked their shipping priority is set to 10.
So the only thing that it could be sorted by is the city.
Thanks,
Shane
Shane, please confirm. You're saying now that we can only sort by one custom field? Is that because I am using Advanced Custom Fields, or would that same limitation be true even if I was using WP Types Custom Fields?
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Keith,
No i'm not saying that. It is possible to sort by 2 custom fields.
The reason why its not working is because I checked on the posts and there was no field called acf_directory_sort_order, only a city field.
The other field that I saw with any ordering was the shipping priority field but this field from what I see most post have 10 as the value.
Thanks,
Shane
Okay, then I guess I am not clear when you there was no field with name acf_directory_sort_order. It is on the Provider custom post type as you can see here - hidden link. And it can be seen here ACF Field Group called "Directory Listing Information" - hidden link.
So, where is it you are seeing the field? It was added later, so maybe some records don't have it recorded in the database record, but the field is definitely there.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Keith,
Here is another revision of the code.
This one i've tested to ensure the values change when i'm trying to sort.
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' => '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 ASC, mt1.meta_value ASC';
}
Please let me know if this helps.
Thanks,
Shane
So, why would it be working on your test site and not on my test site? I've added the code to functions.php in the child theme. Then I went to a state with a smaller sampling than Florida, in this case Nevada - hidden link. And I made sure to update something on each of those records so for sure the custom order field would be saved in the database. And I changed the first record on the page to have a sort value of 200, so it should go to bottom since all others have a sort value of 100. But the record did not go to the bottom.
Am I missing something?
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
I'm logging in to check whats happening here.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Keith,
Could you check the sort order here hidden link
Also this function here you can change the sort order .
function custom_order($orderby) {
global $wpdb;
return $wpdb->postmeta.'.meta_value ASC, mt1.meta_value ASC';
}
You can change it to ASC or DESC
Interesting. I do now see it is working on that page - hidden link. That page is not actually one I use because I don't want to present ALL the providers. So, hence I am using this URL_PARAM that is grabbing a smaller group (in this case a single state) - hidden link. That is where it is not working, but where I need it to work.
We are so close, and I really appreciate your help on this.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Keith,
I'm taking another look at this for.
Hopefully we can resolve this one soon.
Thanks,
Shane
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Could you change the ID that the code should target to 16253 and let me know if it starts working now.
This is very strange. I went to the dev site sample page to test and it actually sorted the way I wanted. I tried two different states
hidden link
hidden link
And that was without changing the ID. If I changed the ID then the order seems totally random. So, then I copied the code onto the functions.php of the live site. But it did not work. Did I need to do something in addition to the just the code on functions.php? Even after clearing cache it didn't work.
Feels like we are so close.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Keith,
Just to ensure that we are on the same page .
This is the view that we should be targetting correct?
hidden link
No, that is the incorrect one. This is the one -
hidden link