Search stores (custom postcode field) within 5, 10, 15 miles of user inputted postcode.. Code modification

I have been searching for a location search solution for wordpress.. The best semi-solution I have found is http://mondaybynoon.com/2010/02/22/store-locator-wordpress-pods

The problem being is that I do not want to use Pods and wish to search in the custom fields of a post instead.

Read More

I have a separate table set up with all UK postcodes + lat, log data..

Here is the function.

function mbn_get_stores_by_location( $postcode, $radius )
{
  global $wpdb;
  $radius = intval( $radius );

  // we first need to get the source coordinates
  $sql = "SELECT `latitude`, `longitude` FROM `uk_postcodes` WHERE `postcode` = '$postcode'";
  $coords = $wpdb->get_row( $sql );

  // now we'll get the other Postcodes within the radius, ordered by distance
  $sql = "SELECT uk_postcodes.postcode, ( 3959 * acos( cos( radians( $coords->latitude ) ) * cos( radians( uk_postcodes.latitude ) ) * cos( radians( uk_postcodes.longitude ) - radians( $coords->longitude ) ) + sin( radians( $coords->latitude ) ) * sin( radians( uk_postcodes.latitude ) ) ) )  AS distance FROM uk_postcodes HAVING distance <= $radius OR distance IS NULL ORDER BY distance";

  $nearby_postcodes = $wpdb->get_results( $sql );

  // we need to store the zips in order to build the Pods query
  $target_postcodes = array();
  foreach ($nearby_postcodes as $nearby_postcode)
  {
    array_push($target_postcodes, $nearby_postcode->postcode);
  }

  // we're going to store the results as we go
  $store_results = array();

  if( count( $target_postcodes > 0 ) )
  {
    $final_target_postcodes = implode(',', $target_postcodes);

    if( strlen( $final_target_postcodes > 0 ) )
    {
      // let's snag the data
      $stores = new Pod('stores');
      $stores->findRecords('id ASC', 9, 't.postcode IN (' . $final_target_postcodes . ')');

      $store_data = array();

      while ( $stores->fetchRecord() )
      {
        $store_data['id']       = $stores->get_field('id');
        $store_data['name']     = $stores->get_field('name');
        $store_data['slug']     = $stores->get_field('slug');
        $store_data['address']  = $stores->get_field('address');
        $store_data['city']     = $stores->get_field('city');
        $store_data['state']    = $stores->get_field('state');
        $store_data['postcode']  = $stores->get_field('postcode');

        foreach ($nearby_postcodes as $nearby_postcode)
        {
          if( $store_data['postcode'] == $nearby_postcode->postcode )
          {
            $store_data['distance'] = intval( $nearby_postcode->distance );
          }
        }

        array_push( $store_results, $store_data );
        unset( $store_data );
      }

      usort( $store_results, "mbn_cmp" );
    }

  }
    return $store_results;
}

I am assuming that I need to change

$stores = new Pod('stores');
 $stores->findRecords('id ASC', 9, 't.postcode IN (' . $final_target_postcodes . ')');

to reflect query_posts.. I have tried many variations of

$stores = query_posts('meta_key=post_code&meta_value=' . $final_target_postcodes . '');

with no avail..

Could some one please give me a pointer in the right direction?

Related posts

Leave a Reply

1 comment

  1. There has been a number of questions and answers on this site regarding returning results based on Geo Location.

    Please take a look at the following posts:

    Is it possible to wrap Geo Location search around WP_Query?

    Optimizing a Proximity-based Store Location Search on a Shared Web Host?

    How can I implement a location based (zip code) search in WordPress?

    I have geocoded posts with latitude longitude – How to search by radius?

    Also please take a look at my plugin: Geo Data Store which I built due to the need for storing geo data in a more optimised fashion than what WordPress does out of the box. You save your coordinates in a custom field of your choice, tell the plugin which field that is by using a filter and then that custom field is copied into a table optimized for radius searching.

    The plugin then gives you a couple of functions you can use to return post ID’s that are in a given radius of a point, which you can then use the post ID’s in a WP_Query or pre_get_posts.