在 Wordpress 中按距离重新排序 post 结果
Re-order post results by distance in Wordpress
已更新
最终代码在这里。我认为它完美无缺。
<?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();
?>
我正在尝试按两种 post 类型之间的距离(坐标)对 post 进行排序。
例如,如果您看到 'Property' post,它显示相关的 'Place' post(在同一城市)。
问题是我不知道 如何按距离 post 结果重新排序 。
这是对当前代码的一些解释。
- 两种类型的 post 都具有地理数据 (latitude/longitude)。
$lat1
和 $lon1
:ACF 字段到位(博客 post)
$lat2
和 $lon2
:属性 中的自定义字段(自定义 post 类型)
当前代码:
// 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();
您将无法直接通过查询执行此操作。您将需要加载位置、计算距离、将位置分配给数组,然后使用 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;
}
已更新 最终代码在这里。我认为它完美无缺。
<?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();
?>
我正在尝试按两种 post 类型之间的距离(坐标)对 post 进行排序。 例如,如果您看到 'Property' post,它显示相关的 'Place' post(在同一城市)。 问题是我不知道 如何按距离 post 结果重新排序 。
这是对当前代码的一些解释。
- 两种类型的 post 都具有地理数据 (latitude/longitude)。
$lat1
和$lon1
:ACF 字段到位(博客 post)$lat2
和$lon2
:属性 中的自定义字段(自定义 post 类型)
当前代码:
// 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();
您将无法直接通过查询执行此操作。您将需要加载位置、计算距离、将位置分配给数组,然后使用 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;
}