更新的 最终代码在这里。我觉得它很有效。
<?php
$my_query = new WP_Query($args_places_cafe);
$locations = array();
if ($my_query->have_posts()) { ?>
<div class="yelp_bussines_wrapper"><div class="yelp_icon"><i class="icon-paw"></i></div>
<h4 class="yelp_category">Cafe</h4>
<?php while ($my_query->have_posts()):$my_query->the_post();
$locationp = get_field('place_address');
$lat1 = $locationp['lat'];
$lon1 = $locationp['lng'];
$lat2 = $gmap_lat;
$lon2 = $gmap_long;
$dist = distance($lat1, $lon1, $lat2, $lon2, "K");
$title = get_the_title();
$locations[] = array(
'title' => $title,
'dist' => round($dist, 1)
);
endwhile;
usort( $locations, 'sort_locations' );
foreach( array_slice($locations, 0, 3) AS $location ) {
print '<div class="yelp_unit"><h5 class="yelp_unit_name">'.$location['title'].'</h5>';
print '<span class="yelp_unit_distance"> ('.$location['dist'].' km)</span></div>';
}
}
function sort_locations( $a, $b ) {
if ( $a['dist'] == $b['dist'] ) {
return 0;
}
return ($a['dist'] < $b['dist'] ) ? -1 : 1;
}
wp_reset_query();
?>
&#13;
我试图按两种帖子类型之间的距离(坐标)命名。 例如,如果您看到&#39;属性&#39;发布,它显示相关的地点&#39;帖子(在同一个城市)。 问题是我不知道如何按距离重新订购帖子结果。
以下是对当前代码的一些解释。
$lat1
和$lon1
:ACF字段到位(博客文章)$lat2
和$lon2
:属性中的自定义字段(自定义帖子类型)当前代码:
// Args for Places
$args_places = array(
'post_type' => 'post',
'category__in' => 168,
'showposts' => '3',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'places-cities',
'terms' => $terms_city,
'field' => 'name'
)
)
);
/**
* Get distance between 'Properties' and 'Places'
*/
function distance($lat1, $lon1, $lat2, $lon2, $unit)
{
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} elseif ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
// Get Places
$my_query = new WP_Query($args_places);
if ($my_query->have_posts()) {
while ($my_query->have_posts()) {
$my_query->the_post();
$location = get_field('place_address');
$lat1 = $location['lat'];
$lon1 = $location['lng'];
$lat2 = $gmap_lat;
$lon2 = $gmap_long;
$dist = distance($lat1, $lon1, $lat2, $lon2, "K");
get_the_title();
echo round($dist, 1).' km';
}
}
wp_reset_query();
答案 0 :(得分:0)
您无法直接使用查询执行此操作。您需要加载场所,计算距离,将位置分配给阵列,然后使用usort对阵列进行排序。
这是您的代码,适用原则。请注意,由于您没有为我们提供足够的信息来构建/测试此内容,这只是关键概念 - 您可能需要根据数据调整内容以使其正常工作等。
// Load places into a custom WP query
$my_query = new WP_Query( $args_places );
// create an array to store the locations in....
$locations = array();
// loop over the query results
if ( $my_query->have_posts() ) {
while ($my_query->have_posts()):
$my_query->the_post();
$location = get_field('place_address');
$lat1 = $location['lat'];
$lon1 = $location['lng'];
$lat2 = $gmap_lat;
$lon2 = $gmap_long;
$dist = distance($lat1, $lon1, $lat2, $lon2, "K");
$title = get_the_title();
// load the values into the array so it's available for sorting
$locations[] = array(
'title' => $title,
'location' => $location
'dist' => $dist
);
endwhile;
// sort the array by distance
usort( $locations, 'sort_locations' );
// NOW you can output the locations, as they are sorted by distance
foreach( $locations AS $location ) {
echo $location['title'];
echo $location['dist'] . 'km';
// ...etc
}
}
// callable for usort. Sorts the array based on the 'dist' array value
function sort_locations( $a, $b ) {
if ( $a['dist'] == $b['dist'] ) {
return 0;
}
return ($a['dist'] < $b['dist'] ) ? -1 : 1;
}