他们说坚持是关键。。。已在相关问题中找到链接。。https://stackoverflow.com/questions/17588525/how-to-join-post-meta-and-taxonomy-table-wpdb-mysql, 我得到了答案。好的,它没有像建议的那样使用本机参数,也没有像想象的那样提供过多的加载时间(它在侧边栏小部件中最多只显示5个属性)。
并不是每个在这里提出问题的人都在寻找能为他们做工作的人,更多的是寻求指导、建设性的指导和帮助。
它并不漂亮,可以编写更好的代码,但对于任何其他正在搜索的人来说。。这可能会有所帮助。
$dist=\'3.427\'; /* Kilometers*/
$orig_lat = get_post_meta ( $post_id, "_property_latitude",true);
$orig_lon = get_post_meta ( $post_id, "_property_longitude",true);
$lon1 = (float) $orig_lon - (int) $dist / abs( cos( deg2rad( (float) $orig_lon ) ) * 69 );
$lon2 = (float) $orig_lon + (int) $dist / abs( cos( deg2rad( (float) $orig_lon ) ) * 69 );
$lat1 = (float) $orig_lat - ( (int) $dist / 69 );
$lat2 = (float) $orig_lat + ( (int) $dist / 69 );
/* the 6371 is Radius of the Earth in kilometers - for miles multiply by 0.621371192 */
$mapsearchquery = "SELECT DISTINCT `t`.`ID`,
6371 * 2 * ASIN( SQRT( POWER( SIN( ( \'".$orig_lat."\' - `t`.`latitude` ) * pi() / 180 / 2), 2 ) + COS( \'".$orig_lat."\' * pi() / 180) * COS( `t`.`latitude` * pi() / 180 ) * POWER( SIN( ( \'".$orig_lon."\' - `t`.`longitude` ) * pi() / 180 / 2 ), 2 ) ) ) AS `distance`
FROM (
SELECT `$wpdb->posts`.`ID`,
MAX(CASE WHEN `$wpdb->postmeta`.`meta_key` = \'_property_longitude\' THEN `$wpdb->postmeta`.`meta_value` END ) AS `longitude`,
MAX(CASE WHEN `$wpdb->postmeta`.`meta_key` = \'_property_latitude\' THEN `$wpdb->postmeta`.`meta_value` END ) AS `latitude`
FROM `$wpdb->posts`
LEFT JOIN `$wpdb->postmeta` ON ( `$wpdb->posts`.`ID` = `$wpdb->postmeta`.`post_id` )
INNER JOIN `$wpdb->term_relationships` ON (`$wpdb->posts`.`ID` = `$wpdb->term_relationships`.`object_id`)
INNER JOIN `$wpdb->term_taxonomy` ON (`$wpdb->term_relationships`.`term_taxonomy_id` = `$wpdb->term_taxonomy`.`term_taxonomy_id`)
INNER JOIN `$wpdb->terms` ON (`$wpdb->terms`.`term_id` = `$wpdb->term_taxonomy`.`term_id`)
WHERE `$wpdb->posts`.`post_status` = \'publish\'
AND `$wpdb->term_taxonomy`.`taxonomy` = \'contract_type\' AND `$wpdb->terms`.`term_id` = (\'".$_contract_type."\')
AND `$wpdb->posts`.`post_type` = \'dt_properties\'
AND `$wpdb->postmeta`.`post_id` != \'".$post_id."\'
GROUP BY `$wpdb->posts`.`ID`
HAVING `longitude` BETWEEN \'".$lon1."\' AND \'".$lon2."\' AND `latitude` BETWEEN \'".$lat1."\' AND \'".$lat2."\') AS `t`
HAVING distance < \'".$dist."\'
ORDER BY distance ASC;";
// Just get the ID\'s
//$pageposts = $wpdb->get_col($mapsearchquery);
$pageposts = $wpdb->get_results($mapsearchquery, OBJECT);