WP Geo search
A plugin to add location-aware geographical search to WP_Query . You can use it to power location-aware apps, such as showing a user results near them. 🔎 Using it in a query Adding a geo_query parameter to WP_Query will add a “distance” column to the returned results, provided they have the right m
A plugin to add location-aware geographical search to WP_Query .
You can use it to power location-aware apps, such as showing a user results near them.
🔎 Using it in a query
Adding a geo_query parameter to WP_Query will add a “distance” column to the returned results, provided they have the right metadata.
You can then display this in your templates.
You can use a location search parameter, which will be geocoded or directly provide latitude and longitude values:
$query = new WP_Query(array( "geo_query" => array( "location" => "London" ) )) $query = new WP_Query(array( "geo_query" => array( "latitude" => -52.005, "longitude" => 0.005, ) ))
Optionally, you can then filter by search radius.
By default, distances are given in miles. You can provide "units" => "km" if you need kilometres.
$query = new WP_Query(array( "geo_query" => array( "latitude" => -52.005, "longitude" => 0.005, "radius" => 10 ) ))
Or order by nearness:
$query = new WP_Query(array( "geo_query" => array( "latitude" => -52.005, "longitude" => 0.005 ), "orderby" => "geo" ))
Displaying distance in templates
In a WP_Query loop that includes a geo_query , you can use two extra functions to show distance away:
- jhgs_get_the_distance(object $post) – which returns a rounded integer for the distance away, similar to get_the_title()
- jhgs_the_distance(string $less_than_one, string $one, string $more_than_one) – which displays an approximate human-readable string, similar to the_title() jhgs_the_distance will show one of three messages depending on whether the rounded distance is less than one, one, or greater than one. By default these are:
- “Less than a mile away”
- “About a mile away”
- “About %s miles away”
If you need to use different units or translations, can pass three printf-formatted strings to jhgs_the_distance() to override these messages. Put %s where you want the value.
If you need the exact , unrounded value, you can use $post->distance .
Geocoding
Nominatim ‘s service is used for geocoding location searches.
Using it is subject to an acceptable use policy – if you use case will involve lots of API calls, you should replace it with a paid alternative, like Google ‘s.