Fetching posts ordered by distance between input coordinates and ACF Google Map field data #2695
Replies: 1 comment
-
Hey @Ririshi In general, the best forward-compatible way would be to add That said, since the haversine distance varies depending on the provided location (which I'm assuming you are also adding as where args on the CPT), you need to change how WPQuery itself works ( e.g.. ). If you're creating a custom class like in the example (or using a 3rd party plugin for querying by geoloc), then you'd filter the connection resolver to use your custom class instead of the default WPQuery (see the |
Beta Was this translation helpful? Give feedback.
-
I'm trying to fetch a CPT's posts ordered by distance from a given set of coordinates. The CPT has an ACF Google Map field, which holds a latitude and longitude. I'm trying to order the posts by the haversine distance computed between those coordinates, and the coordinates given as a query argument. I'd like to extend the WPGraphQL schema by either adding a new field on the RootQuery, or by extending the existing post type plural field.
So far, I came up with a way to compute haversine distance as a field I added to the "distributors" by using a custom input type, as well as a
distanceTo
field, added using thegraphql_object_fields
filter. With the query below, I can fetch distributors (the CPT in question), with the distance to the given coordinates, but I cannot order the output by that field without fetching every distributors and sorting the result client-side.I'm hoping someone knows how I can incorporate the distance computation and subsequent sorting in a way where I can get the closest
n
distributors given a set of coordinates.Beta Was this translation helpful? Give feedback.
All reactions