从FacetWP邻近函数提取距离以在Wordpress模板中显示 [英] Extracting Distance from FacetWP Proximity Function to Display in Wordpress Template

查看:142
本文介绍了从FacetWP邻近函数提取距离以在Wordpress模板中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Wordpress&通常,在php编码中,我一直在使用一个相对简单的主题,其中包含存储纬度和经度的自定义帖子类型.每个帖子都是目录中的酒店列表.我正在使用的主题也与FacetWP集成了WordPress的faceting插件.用户可以通过键入自己的位置来搜索站点,这是使用Google Maps功能实现的.提交搜索后,参数将传递到存档页面:

I am quite new to Wordpress & Php coding in general, I have a relatively simple theme that I have been working with that contains a custom post type that stores latitude and longitude. Each post is a listing a hotel listing within a directory. The theme I am using also integrates with FacetWP the faceting plugin for WordPress. It is possible for the user to search the site by typing a location of locating themselves, this happens using Googles Mapping functionality. When the search is submitted parameters are passed to an archive page:

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

43.653226,-79.38318429999998,10,Toronto%2C%20ON%2C%20Canada

因此表格将档案的中心纬度和经度以及半径(10英里)传递给档案.

So form passes the archive the central latitude and longitude as well at the radius (10 miles).

然后,归档文件使用此信息来查询帖子类型列表,以返回附近的位置.

The archive then uses this information to query the post type listings to return nearby locations.

我真正想做的是在存档/帖子概述页面中显示每个帖子与中心位置的距离.

这应该是可能的,因为主题具有按距离排序的选项.我只是想不出如何获取计算值并在页面正文中回显它.接近功能的php是:

This should be possible as the theme has a sort by distance sort option. I just can't work out how to grab the calculated value and echo it in the page body. the php for the proximity function is:

<?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并进行了尝试,但由于距离不是存储​​的值,而是计算的结果,因此似乎不是行之有效的方法.

Any assistance you can offer would be appreciated, I have tried reading up on wpquery and experimenting with that, but does not seem to be the way to go as distance is not a stored value, rather it is a calculation.

推荐答案

我从Matt@wordpress.stackexchange收到并回答了我的问题.实际上,有一个FacetWP挂钩可以增加到页面模板的距离.功能过滤器:

I received and answer to my question from Matt@wordpress.stackexchange. There is actually a FacetWP hook to add distance to page templates. A functions filter:

add_filter( 'facetwp_proximity_store_distance', '__return_true' );

和一些php以启用该功能:

and a bit of php to enable the functionality:

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

我希望这对某人有帮助,再次感谢马特

I hope this helps somebody, thanks again Matt

这篇关于从FacetWP邻近函数提取距离以在Wordpress模板中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆