Support

Account

Home › Forums › ACF PRO › Create an API endpoint with JavaScript variable to sort Google Map locations

Unread

Create an API endpoint with JavaScript variable to sort Google Map locations

  • I am using a React frontend which utilizes the WordPress API V2 in order to return store locations I am building with ACF fields. The API endpoint URL looks like this:

    https://example.com/wp-json/wp/v2/stores

    This is correctly returning my ACF fields with my custom post type of “stores.” The problem is that I have a lot of these stores, so the performance is not so great since I am currently sorting the response on the frontend, which requires me to query all the posts at once. What I am wondering is if there is any way for me to create a custom API endpoint which would return the stores in order of distance from a given location(JavaScript variable in React) which would be passed in through the API call and then calculated based off the lat/lng coordinates from the Google Maps fields I have set for each store location. The API URL would look something like:

    https://example.com/wp-json/wp/v2/stores?center=39.8283,-98.5795

    I’ve found quite a few examples of how to do this in PHP, but haven’t found exactly what I’m looking for in terms of making an API request like this. I found this gist, which seems to be in the same vein of what I’m looking for, but it’s 4 years old and I’m not quite sure how it works or if it is even what I need.

    Any ideas? Thank you!

Viewing 1 post (of 1 total)

The topic ‘Create an API endpoint with JavaScript variable to sort Google Map locations’ is closed to new replies.