【发布时间】:2016-04-02 05:51:09
【问题描述】:
我正在对帖子执行距离明智的排序。场景是这样的:用户输入一些城市名称,我得到城市的坐标。每个帖子也有一些坐标作为postmeta。我需要找到这两个点之间的距离并对帖子进行排序,例如最低距离帖子将首先显示。
我尝试了以下代码来计算距离,效果很好。我的问题是将这个距离附加到帖子上。我尝试将属性添加到 post 对象。但是然后如何对这些帖子进行排序?
我需要带有排序帖子的 WP_Query 对象。
$prop_selection = new WP_Query($args);
while ($prop_selection->have_posts()): $prop_selection->the_post();
$property_lat = get_post_meta($post->ID,'property_latitude',true);
$property_lng = get_post_meta($post->ID,'property_longitude',true);
$distancefromcity=distance($property_lat,$property_lng,$city_lat,$city_lng,"K");
$distancefromcity=round($distancefromcity,2);
$post = (array)$post;
$post['distance'] = $distancefromcity;
$post = (object)$post;
endwhile;
【问题讨论】: