Support

Account

Home Forums Front-end Issues Order ACF taxonomy terms by user's current distance from location

Helping

Order ACF taxonomy terms by user's current distance from location

  • 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!

  • I am not 100% sure what you are asking.

    for get terms

    
    $args['include'] = array(/* array of term IDs in the order you want them returned */);
    $args['orderby'] = 'include';
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.