简体   繁体   English

WP_Query使用派生变量作为排序依据

[英]WP_Query use derived variable as order by

I am trying to order my posts by a value that is derived from getting the user's lat/long location information, and the same information for the post. 我正在尝试通过一个值来排序我的帖子,该值是从获取用户的经/纬度位置信息以及该帖子的相同信息中得出的。

I already have the functions in place to get the user and post distance, so do not need that, but need to know how to code my args for wp_query. 我已经具有获取用户和发布距离的功能,因此不需要它,但是需要知道如何为wp_query编码我的args。

My initial wp_query is 我最初的wp_query是

 array(
  "post_type" => "babysitters",
  "post_status" => "publish",
  "orderby" => "date",
  "order" => "DESC",
  "posts_per_page" => -1
);

I have a function that returns the distance value between the current user and the post, and would like to use this value to change the default sort order. 我有一个函数,该函数返回当前用户与帖子之间的距离值,并希望使用该值来更改默认的排序顺序。

Here is my code for finding the distance (which works without issues) 这是我的寻找距离的代码(可以正常工作)

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);
  } else if ($unit == "N") {
      return ($miles * 0.8684);
    } else {
        return $miles;
      }
}

function user_distance_compare() {

$user_id = bp_loggedin_user_id();
$user_location = get_user_meta($user_id, 'geocode_321', false); 
$user_loc = implode(',', $user_location);
$commapos = strpos($user_loc, ",");
$loc_len = strlen($user_loc);
$user_lat = substr($user_loc,0,$commapos);
$user_lng = substr($user_loc,($commapos + 1), ($loc_len - $commapos));
$post_id = get_the_ID();
$post_location = get_field('suburb', $post->ID);
$post_lat = $post_location['lat'];
$post_lng = $post_location['lng'];
//var_dump($user_lat);
//var_dump($user_lng);
//var_dump($post_lat);
//var_dump($post_lng);
$distance = distance($user_lat, $user_lng, $post_lat, $post_lng, "K");
return $distance;
}

How do I assign the returned distance so it matches with a post ID, that I can then use this derived distance to do my sort? 如何分配返回的距离,使其与帖子ID匹配,然后可以使用此派生的距离进行排序?

IE Post Distance IE发布距离

  • 1 - 33.4 1-33.4
  • 2 - 14.2 2-14.2
  • 3 - 1.7 3-1.7
  • 4 - 12.5 4-12.5

In the end I'd like to have it displayed in distance order, so that it would be 3,4,2,1 as the post order. 最后,我想按距离顺序显示它,这样它作为后继顺序将为3,4,2,1。

By SQL it would read Select DISTINCT post_id, distance (where it has been derived from the post information) from posts where post_type = 'babysitters' order by distance 通过SQL,它将从中按距离读取post_type ='babysitters'的帖子中读取Select DISTINCT post_id,即距离(已从帖子信息中得出)

Anyone able to help? 有人能帮忙吗?

If you're getting the distance using a function, I'd built an array of post IDs sorted by distance. 如果您使用函数获取距离,则可以构建一个按距离排序的帖子ID数组。 Then use the 'post__in' parameter of WP_Query to get only those items that you want. 然后,使用WP_Query的'post__in'参数仅获取所需的那些项目。 You can also use 'post__in' for the orderby parameter. 您也可以将'post__in'用作orderby参数。

$babysitters = array(
  '10' => 23,
  '12' => 345,
  '18' => '27
)

get_posts(array(
  'post_type' => 'babysitter',
  'posts_per_page' => -1,
  'post__in' => $babysitters,
  'orderby' => 'post__in'
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM