【问题标题】:Wordpress Sort the posts returned by WP_QueryWordpress 对 WP_Query 返回的帖子进行排序
【发布时间】: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;

【问题讨论】:

    标签: php wordpress sorting


    【解决方案1】:
    1. $distancefromcity 添加到帖子meta-data

    2. 进行自定义选择查询并按距离排序。看 http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

    【讨论】:

    • 谢谢。我以其他方式做到了。我已将其发布在答案中。请对此提出任何建议。
    【解决方案2】:

    我是按照以下方式完成的。 这样可以吗还是有更好的方法?

        while ($prop_selection->have_posts()): $prop_selection->the_post(); 
    
            $property_lat = 0;
            $property_lng = 0;
    
            $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);
            $distance_array[]= array( 'ID' => $post->ID,
                                    'distance' => $distancefromcity);
    
        endwhile;
    
    
        usort($distance_array, function ($item1, $item2) {
            if ($item1['distance'] == $item2['distance']) return 0;
            return $item1['distance'] < $item2['distance'] ? -1 : 1;
        });
    
    
        $sorted_posts = array();
    
        foreach($distance_array as $key)
        {
            $sorted_posts[]=$key['ID'];
        }
    
    
        $args = array(
            'cache_results'           =>    false,
            'update_post_meta_cache'  =>    false,
            'update_post_term_cache'  =>    false,
            'post_type'               =>    'estate_property',
            'post_status'             =>    'publish',
            'paged'                   =>    $paged,
            'posts_per_page'          =>    $prop_no,
            'post__in'                =>    $sorted_posts,
            'orderby'                 =>    'post__in'
        );
    
        $prop_selection =   new WP_Query($args);
    

    【讨论】:

    • 我认为这很好。但是请记住,您将对为您的站点发出的每个请求进行这种排序,我认为这是不必要的开销。如果您使用 post 元数据方法,则每个请求只有一个查询。
    • 实际上用户会搜索多个城市。所以添加 postmeta 可能会增加更多开销?(当我说“用户输入城市名称”时,我的意思是他会搜索多次,而不是第一次。)
    • 在这种情况下,你的方式更有意义!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    相关资源
    最近更新 更多