franc-liuzzi / ep-geo
ElasticPress 的地理查询集成
0.2.2
2021-07-27 14:31 UTC
Requires
- illuminate/support: ^8.51
Requires (Dev)
- 10up/elasticpress: ^3.6
- friendsofphp/php-cs-fixer: ^3.0
- phlak/semver-cli: ^1.0
README
ElasticPress 的地理查询集成
安装
- 请按照 ElasticPress 的安装说明进行。
- 在 WordPress 中安装并激活此插件(ep-geo)。
- 导航到管理 > ElasticPress 并激活 "地理"。
用法
默认情况下,此插件会在名为 "latitude" 和 "longitude" 的文章元字段中查找。它们应该是纯文本字段,其中纬度和经度以浮点数表示。
查找距离美国俄勒冈州波特兰30英里范围内的帖子,按距离排序
new WP_Query( array( 'ep_integrate' => true, 'posts_per_page' => 100, 'post_type' => 'post', 'orderby' => 'geo_distance', 'order' => 'asc', 'geo_distance' => array( 'distance' => '30mi', 'geo_point.location' => array( 'lat' => 45.5231, 'lon' => -122.6765, ), ), ) );
如果您的纬度和经度数据存储在其他地方,或者需要计算或预处理 geo_point 位置,可以通过 WordPress 钩子进行配置
/** * Alter geo_point location to use my_lat/my_lon. */ add_filter( 'ep_geo_post_sync_geo_point', function ( $geo_point, $post_args, $post_id ) { $geo_point['location']['lat'] = get_field( 'my_lat', $post_id ); $geo_point['location']['lon'] = get_field( 'my_lon', $post_id ); return $geo_point; }, 10, 3 );