我一直在为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 . \'\');
没有用。。
谁能给我一个指向正确方向的指针吗?