根据距离对不同类型的帖子进行排序的最佳方法

时间:2016-07-27 作者:Amino

我正在做一个项目

我们有x个帖子,1个赞助商(自定义元域),无限付费,无限免费帖子(这些都是帖子类型)

  • 我们需要在顶部显示1个赞助商,无限制后付费,无限制后付费

    我们需要根据用户选择的区域的距离来订购所有产品

    每篇文章都有存储在数据库中的位置

  • Question #1:

    目前,我用3个不同的$参数进行了3个不同的查询:

     $args_sponsored = array(
                            \'post_type\' => array(\'le_place\'),
                            \'posts_per_page\' => \'1\',
                            \'tax_query\' => array(
                                array(
                                    \'taxonomy\' => \'le_place_category\',
                                    \'field\' => \'slug\',
                                    \'terms\' => array($theCat)
                                )
                            ),
                            \'meta_query\' => array(
                                array(
                                    \'key\' => \'entry_type\',
                                    \'value\' => \'sponsored\',
                                )
                            )
                        );
    
                          $args_paid = array(
                            \'post_type\' => array(\'le_place\'),
                            \'posts_per_page\' => -1,
                            \'order\' => \'DESC\',
                            \'tax_query\' => array(
                                array(
                                    \'taxonomy\' => \'le_place_category\',
                                    \'field\' => \'slug\',
                                    \'terms\' => array($theCat)
                                )
                            ), 
                            \'meta_query\' => array(
                                array(
                                    \'key\' => \'entry_type\',
                                    \'value\' => array(\'paid\'),
                                )
                            )
                        );
    
    
                        $args_free = array(
                            \'post_type\' => array(\'le_place\'),
                            \'posts_per_page\' => -1,
                            \'order\' => \'DESC\',
                            \'tax_query\' => array(
                                array(
                                    \'taxonomy\' => \'le_place_category\',
                                    \'field\' => \'slug\',
                                    \'terms\' => array($theCat)
                                )
                            ), 
                            \'meta_query\' => array(
                                array(
                                    \'key\' => \'entry_type\',
                                    \'value\' => array(\'free\'),
                                )
                            )
                        );
    

    and not sure this affect performance! is that a wrong method?

    Question #2:在获得所有帖子后,如何按距离对它们进行排序,然后显示它们?

    sortByDistance($postsPaid); // how can I get custom fields of post objects?

     $postsPaid = $queryPaid->get_posts();
                              sortByDistance($postsPaid); // how can I get custom fields of post objects? 
    
    
                                $placeLocation= get_field(\'location\');
                                   $longPlace =$placeLocation[\'lng\'];
    
                             $latPlace = $placeLocation[\'lat\'];
    
    
    
     function getDistance($lat2, $long2, $lat1, $long1) {
                            $R = 6378137; // Earth’s mean radius in meter
                            $dLat = rad($lat2 - $lat1);
                            $dLong = rad($long2 - $long1);
    
                          $a = sin($dLat / 2) * sin($dLat / 2) +
                                    cos(rad($lat1)) * cos(rad($lat2)) *
                                    sin($dLong / 2) * sin($dLong / 2);
                            $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
                            $d = $R * $c; // in meter
    
                            return intval((ceil($d) / 1000) * 0.621371); // returns the distance in Miles
                     };
    

    1 个回复
    SO网友:Amino

    好的,我添加了距离作为自定义字段,以允许我使用wp\\u query orderby方法对其进行排序!

     function    add_distance_as_customfield($posts){
    
           $longDest = $_GET[\'lng\'];
                        $latDest = $_GET[\'ltd\'];
          foreach ($posts as $post) {
               $placeLocation= get_field(\'location\',$post->ID);
                                   $longPlace =$placeLocation[\'lng\'];
    
                             $latPlace = $placeLocation[\'lat\'];
                             $distance=getDistance($latDest,$longDest,$latPlace,$longPlace);
    
                             if ( ! add_post_meta($post->ID, "distance", $distance,true ) ) { 
       update_post_meta( $post->ID, "distance", $distance );
    }
    
          }
     }
    

    相关推荐

    WooCommerce sort by SKU

    所以我试过了https://gist.github.com/bekarice/1883b7e678ec89cc8f4d 在我的网站上。只要所有产品都有SKU,效果就很好。如果产品没有SKU,则根本不会显示。也就是说,是否有其他方法可以按SKU排序,但不要求产品实际具有SKU?