Php 扩展WP查询并返回$post中的输出

Php 扩展WP查询并返回$post中的输出,php,mysql,wordpress,Php,Mysql,Wordpress,我正在尝试创建一个活动站点。目前,用户可以使用谷歌地理编码在其所选搜索位置的指定半径内搜索事件 我有一个自定义的DB查询,它工作得很好,只是我希望事件的距离也显示在站点前端,这样用户就知道每个事件离他们有多远 以下是我目前拥有的自定义查询功能: function location_posts_where( $where ) { // if user has input a location if( isset($_POST['where'])

我正在尝试创建一个活动站点。目前,用户可以使用谷歌地理编码在其所选搜索位置的指定半径内搜索事件

我有一个自定义的DB查询,它工作得很好,只是我希望事件的距离也显示在站点前端,这样用户就知道每个事件离他们有多远

以下是我目前拥有的自定义查询功能:

function location_posts_where( $where ) {  

            // if user has input a location
            if( isset($_POST['where']) != '' ) :

                // check the geo locaton of user
                $geoURL = "http://freegeoip.net/json/". $_SERVER['REMOTE_ADDR'] .'';
                $geo = json_decode(file_get_contents($geoURL), true);

                // Geocode input value + users country code
                $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($_POST['where'])."+".$geo[country_code]."&sensor=false";
                $location = json_decode(file_get_contents($url), true);

                // store lat and long of searched location
                $Slat = $location[results][0][geometry][location][lat];
                $Slng =  $location[results][0][geometry][location][lng];

            // if use hasn't unput a location
            else :

                // get location of user
                $url = "http://freegeoip.net/json/". $_SERVER['REMOTE_ADDR'] .'';
                $geo = json_decode(file_get_contents($url), true);

                // store lat and long of user location
                $Slat = $geo[latitude];
                $Slng = $geo[longitude];

            endif;


            // Specify the co-ordinates that will form  
            // the centre of our search  
            $lat = $Slat;  
            $lng = $Slng; 

            $radius = $_POST['distance']; // (in miles)  

            // Append our radius calculation to the WHERE  
            $where .= " AND wp_posts.ID IN (SELECT post_id FROM lat_lng_post WHERE 
                 ( 3959 * acos( cos( radians(" . $lat . ") ) 
                                * cos( radians( lat ) ) 
                                * cos( radians( lng ) 
                                - radians(" . $lng . ") ) 
                                + sin( radians(" . $lat . ") ) 
                                * sin( radians( lat ) ) ) ) <= " . $radius . ")";

            // Return the updated WHERE part of the query  
            return $where;

        }
这段代码工作得很好,但是,我以前使用过wp_geo_posts插件,它允许您在屏幕上输出每个事件与用户位置之间的距离


使用此代码,$post变量没有“距离”输出。有没有办法将其输出到$post以便在前端显示?

您的查询中可能没有返回该值。为什么不手动构造整个查询?也许最好将距离设置为post meta,在普通安装中,您确实无法向post对象添加任何内容,您必须使用post_meta定义自定义/外部数据与post对象之间的关系。哦,好的,我在一些脚本中看到,人们使用距离作为示例,然后在post对象中引用“距离”。如果距离是在后元中,这可能吗?