title: Querying custom post types by long/lat to find nearest
body: <p>I have a website that allows users to enter their postcode and then they see nearby restaurants. I store the restaurants in a MySQL table and each row contains the restaurant name, address, opening hours, plus longitude and latitude.</p>
  
  <p>When a user enters their postcode I use a Google API to convert the postcode to longitude and latitude and then I use this SQL query ...</p>
  
  <pre><code>select 
      locations.*, 
      SQRT(
          69.1*69.1*(latitude - $latitude)*(latitude - $latitude) + 
          53*53*(longitude - $longitude)*(longitude - $longitude)
      ) as distance 
  from locations 
  having distance &lt;= 100 
  order by distance 
  limit 100
  </code></pre>
  
  <p>I am now migrating the site to Wordpress. I have created a custom post type called restaurants and have used Advanced Custom Fields to record the same fields - name, address, opening hours and longitude and latitude. I am storing the longitude and latitude in separate fields.</p>
  
  <p>I can make use of <code>WP_Query</code> to query all the restaurants:</p>
  
  <pre><code>$query = new WP_Query( array( 'post_type' =&gt; 'restaurants' ) );
  </code></pre>
  
  <p>But how can I amend my <code>WP_Query</code> call to allow me to search and sort restaurants by proximity to the supplied longitude and latitude?</p>
  
  <hr>