在用户输入邮政编码的5、10、15英里范围内搜索商店(自定义邮政编码字段)。代码修改

时间:2011-12-04 作者:Danny

我一直在为wordpress搜索位置搜索解决方案。。我找到的最好的半解决方案是http://mondaybynoon.com/2010/02/22/store-locator-wordpress-pods

问题是我不想使用Pods,而是希望在帖子的自定义字段中搜索。

我有一个单独的表格,上面有所有英国邮政编码+lat、日志数据。。

下面是函数。

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;
}
我认为我需要改变

$stores = new Pod(\'stores\');
 $stores->findRecords(\'id ASC\', 9, \'t.postcode IN (\' . $final_target_postcodes . \')\');
要反映query\\u帖子。。我尝试了许多不同的

$stores = query_posts(\'meta_key=post_code&meta_value=\' . $final_target_postcodes . \'\');
没有用。。

谁能给我一个指向正确方向的指针吗?

1 个回复
SO网友:Scott

关于基于地理位置返回的结果,该网站上有许多问题和答案。

请查看以下帖子:

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?

另外,请查看我的插件:Geo Data Store 我之所以创建它,是因为需要以比WordPress开箱即用的方式更优化的方式存储地理数据。将坐标保存在您选择的自定义字段中,通过使用过滤器告诉插件哪个字段是自定义字段,然后将该自定义字段复制到为半径搜索优化的表中。

然后,该插件为您提供了几个函数,您可以使用这些函数返回位于给定点半径内的帖子ID,然后您可以在WP\\u查询或pre\\u get\\u帖子中使用帖子ID。

结束