Skip to Main Content

Search by Location with WP Query

I get a lot of client requests for “Store Locators” or “Location Finders”. There are some plugins like MapPress and WP Store Locator that work well for simple Location Searches, but as soon as the client has requests for specific functionality or designs, these pre-made options start to fall short, and you end up spending more time fighting their code than writing your own.

What makes it especially frustrating is that, using the Google Maps API most of the features of a store locator or location finder are fairly easy to write. The only hard part is getting posts that are within a certain distance from a specific latitude and longitude. This requires a somewhat complicated trigonometric formula called The Haversine Formula, to calculate distance between two points on a sphere.  All the other filters and selections can be handled with a simple WP Query, but distance is a trickier beast.

ADVERTISEMENT:

Most of the existing plugins that I’ve seen do a great job at the distance calculation, but don’t include much of a framework for building more complicated queries that match post meta and taxonomies. So I wrote one that comes at things from the other direction. It takes the standard WP Query that offers so much flexibility, and adds a geo_query option that will calculate distance from a point.

You use it like this:

<?php
$query = new WP_Query(array(
	// ... include other query arguments as usual
	'geo_query' => array(
		'lat_field' => '_latitude',  // this is the name of the meta field storing latitude
		'lng_field' => '_longitude', // this is the name of the meta field storing longitude 
		'latitude'  => 44.485261,    // this is the latitude of the point we are getting distance from
		'longitude' => -73.218952,   // this is the longitude of the point we are getting distance from
		'distance'  => 20,           // this is the maximum distance to search
		'units'     => 'miles'       // this supports options: miles, mi, kilometers, km
	),
	'orderby' => 'distance', // this tells WP Query to sort by distance
	'order'   => 'ASC'
));

In addition, there are two included functions that can be used to retrieve distance in the resulting loop: the_distance() and get_the_distance(). Both support two optional variables: a post object, and the number of integers to round the distance to.

The full code for the plugin is included below.

2 Comments

    • gschoppe's profile image.

      I’m working on a plugin to simplify the process, but in general, you would use the google maps api (i like using gmaps.js to simplify the API calls) to geocode a search field, into latitude and longitude, then, once you have lat and lng, you send them to an ajax function that runs the query from this post.

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>