2015-10-28 33 views
2

我對Wordpress相當陌生& Php編碼一般來說,我有一個相對簡單的主題,我一直在使用它,包含一個存儲經度和緯度的自定義帖子類型。每篇文章都列出了一個目錄中的酒店列表。我使用的主題也與FacetWP整合WordPress的面向插件。用戶可以通過鍵入自己的位置來搜索網站,這發生在使用Google Maps功能。當搜索提交的參數傳遞到存檔頁面:從FacetWP接近函數提取距離在Wordpress模板中顯示

http://localhost:8888/wowgoddess/listings/?fwp_location=43.653226%2C-79.38318429999998%2C10%2CToronto%252C%2520ON%252C%2520Canada&fwp_sort=distance

解碼該出來爲:

43.653226,-79.38318429999998,10,多倫多%2C%20ON%2C%20Canada

所以表格在半徑(10英里)處通過歸檔中心的經緯度。

存檔然後使用此信息查詢帖子類型列表以返回附近的位置。

我真正想做的是顯示每個帖子從中心位置到存檔/帖子概覽頁面的距離。

這應該是可能的,因爲主題有按距離排序選項排序。我只是無法弄清楚如何獲取計算出的值並在頁面主體中回顯。爲接近函數的PHP是:

<?php 

if (class_exists('FacetWP_Facet_Proximity')) { 
return; 
} 

class FacetWP_Facet_Proximity 
{ 

/** 
* The ordered array of post IDs 
*/ 
public $ordered_posts = array(); 


/** 
* An array containing each post ID and its distance 
*/ 
public $distance = array(); 


function __construct() { 
    $this->label = __('Proximity', 'fwp'); 

    add_filter('facetwp_index_row', array($this, 'index_latlng'), 10, 2); 
    add_filter('facetwp_sort_options', array($this, 'sort_options'), 1, 2 ); 
    add_filter('facetwp_filtered_post_ids', array($this, 'sort_by_distance'), 10, 2); 
} 


/** 
* Generate the facet HTML 
*/ 
function render($params) { 

    $output = ''; 
    $facet = $params['facet']; 
    $value = $params['selected_values']; 
    $unit = empty($facet['unit']) ? 'mi' : $facet['unit']; 

    $lat = empty($value[0]) ? '' : $value[0]; 
    $lng = empty($value[1]) ? '' : $value[1]; 
    $chosen_radius = empty($value[2]) ? '' : $value[2]; 
    $location_name = empty($value[3]) ? '' : urldecode($value[3]); 

    $radius_options = apply_filters('facetwp_proximity_radius_options', array(10, 25, 50, 100, 250)); 

    ob_start(); ?> 
    <input type="text" id="facetwp-location" value="<?php echo $location_name;  ?>" placeholder="<?php _e('Enter location', 'fwp'); ?>" /> 

    <select id="facetwp-radius"> 
     <?php foreach ($radius_options as $radius) : ?> 
     <?php $selected = ($chosen_radius == $radius) ? ' selected' : ''; ?> 
     <option value="<?php echo $radius; ?>"<?php echo $selected; ?>><?php echo "$radius $unit"; ?></option> 
     <?php endforeach; ?> 
    </select> 

    <div style="display:none"> 
     <input type="text" class="facetwp-lat" value="<?php echo $lat; ?>" /> 
     <input type="text" class="facetwp-lng" value="<?php echo $lng; ?>" /> 
    </div> 
<?php 
    return ob_get_clean(); 
} 


/** 
* Filter the query based on selected values 
*/ 
function filter_posts($params) { 
    global $wpdb; 

    $facet = $params['facet']; 
    $selected_values = $params['selected_values']; 
    $unit = empty($facet['unit']) ? 'mi' : $facet['unit']; 
    $earth_radius = ('mi' == $unit) ? 3959 : 6371; 

    if (empty($selected_values) || empty($selected_values[0])) { 
     return 'continue'; 
    } 

    $lat = (float) $selected_values[0]; 
    $lng = (float) $selected_values[1]; 
    $radius = (int) $selected_values[2]; 

    $sql = " 
    SELECT DISTINCT post_id, 
    ($earth_radius * acos(cos(radians($lat)) * cos(radians(facet_value)) * cos(radians(facet_display_value) - radians($lng)) + sin(radians($lat)) * sin(radians(facet_value)))) AS distance 
    FROM {$wpdb->prefix}facetwp_index 
    WHERE facet_name = '{$facet['name']}' 
    HAVING distance < $radius 
    ORDER BY distance"; 

    $this->ordered_posts = array(); 
    $this->distance = array(); 

    if (apply_filters('facetwp_proximity_store_distance', false)) { 
     $results = $wpdb->get_results($sql); 
     foreach ($results as $row) { 
      $this->ordered_posts[] = $row->post_id; 
      $this->distance[ $row->post_id ] = $row->distance; 
     } 
    } 
    else { 
     $this->ordered_posts = $wpdb->get_col($sql); 
    } 

    return $this->ordered_posts; 
} 

您可以提供將不勝感激,我曾嘗試在wpquery閱讀並與試驗的任何援助,但似乎並沒有要走的路是距離不一個儲值,而不是一個計算。

回答

1

我收到並從[email protected]回答了我的問題。實際上有一個FacetWP鉤子來增加頁面模板的距離。一個功能過濾:

add_filter('facetwp_proximity_store_distance', '__return_true'); 

和一點PHP來實現的功能:再次

$distance = facetwp_get_distance(); 
if (false !== $distance) { 
echo round($distance, 1); 
echo " miles"; 
} 

我希望這可以幫助別人,感謝馬特