Hi,
I built a web app where users are workers and building sites are taxonomies, each term having a single ACF Google Maps field to save site’s latitude and longitude.
I made it possible to workers to edit their own working days entries, using acf_form() in front end, letting them choose the day’s building site from a dropdown managed by ACF taxonomy field.
Using Geolocation_API I’d like to order the dropdown list by the current user’s distance from each site, ordering building sites by the nearest to the farthest.
Because user’s location is a temporary value by definition, I’m struggling to get how to compare the ACF stored building sites coordinates values with the user’s lat+long and return a ordered by distance terms list on the front end drop down.
Using ACF taxonomy query filters I can get into the query I need, as per documentation:
add_filter('acf/fields/taxonomy/query', 'my_acf_fields_taxonomy_query', 10, 3);
function my_acf_fields_taxonomy_query( $args, $field, $post_id ) {
// Show 40 terms per AJAX call.
$args['number'] = 40;
// Order by most used.
$args['orderby'] = 'count';
$args['order'] = 'DESC';
return $args;
}
As long as I already have each building site latitude and longitude, and the current user’s coordinates as well, I suppose I need to use usort() function to change the OrderBy output.
Is this the right solution and could anyone suggest me the right sintax to obtain something like:
$args[‘orderby’] = “my_usort_result()”;
Thanks!