[WordPress] 外掛分享: WP Geo search

WordPress 外掛 WP Geo search 的封面圖片。

前言介紹

  • 這款 WordPress 外掛「WP Geo search」是 2020-12-12 上架。
  • 目前有 70 個安裝啟用數。
  • 上一次更新是 2021-09-06,距離現在已有 1336 天。超過一年沒更新,安裝要確認版本是否可用。以及後續維護問題!
  • 外掛最低要求 WordPress 4.0 以上版本才可以安裝。
  • 外掛要求網站主機運作至少需要 PHP 版本 7.0 以上。
  • 有 2 人給過評分。
  • 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。

外掛協作開發者

jhackett1 |

外掛標籤

geo | distance | location | haversine | nominatim |

內容簡介

一款可以為 WP_Query 添加位置感知地理搜索功能的外掛程式。
可用於開發位置感知型應用程序,例如向用戶顯示其周圍的結果。
🔎 在查詢中使用
將“geo_query”參數添加到 WP_Query 中,將在返回的結果中添加一列“距離”,前提是它們具有正確的元數據,然後您可以在模板中顯示此內容。
您可以使用位置搜索參數,它將進行地理編碼或直接提供緯度和經度值:
$query = new WP_Query(array(
"geo_query" => array(
"location" => "倫敦"
)
))

$query = new WP_Query(array(
"geo_query" => array(
"latitude" => -52.005,
"longitude" => 0.005,
)
))

您還可以選擇性地按搜索半徑過濾。
預設情況下,距離以英里為單位,如果您需要公里,可以提供“units”=>“km” 。
$query = new WP_Query(array(
"geo_query" => array(
"latitude" => -52.005,
"longitude" => 0.005,
"radius" => 10
)
))

或按接近程度排序:
$query = new WP_Query(array(
"geo_query" => array(
"latitude" => -52.005,
"longitude" => 0.005
),
"orderby" => "geo"
))

顯示模板中的距離
在包含geo_query的 WP_Query 循環中,您可以使用兩個額外的函數顯示相應距離:

jhgs_get_the_distance(object $post) –返回距離的四捨五入整數,類似於get_the_title()

jhgs_the_distance(string $less_than_one, string $one, string $more_than_one) – 顯示一個大約以人類能讀的字符串, 類似於the_title()

jhgs_the_distance將根據捨棄後的距離顯示三條消息中的其中一個。
默認情況下,它們為:

“不到一英里的距離”

“大約一英里的距離”
“大約 %s 英里之遙”

如果您需要使用不同的單位或翻譯,可以將三個printf格式的字符串傳遞給jhgs_the_distance(),以覆蓋這些消息。在此,您將%s放在所需值的位置。
如果您需要精確的未捨棄的數據,可以使用$post->distance。

地理編碼

使用Nominatim服務對位置搜索進行地理編碼。
使用它取決於一個可接受的使用策略--如果您的用例涉及大量的 API 調用,應該將它替換為付費方案,例如 Google 的。

📍 填充緯度和經度數據
尋找文章上鍵為“latitude”和“longitude”的兩個自訂字段值。
對於如何提供此類數據,他是不持有任何偏見的,最簡單的方法是直接在文本編輯器中輸入。

原文外掛簡介

A plugin to add location-aware geographical search to WP_Query.
You can use it to power location-aware apps, such as showing a user results near them.
🔎 Using it in a query
Adding a geo_query parameter to WP_Query will add a “distance” column to the returned results, provided they have the right metadata.
You can then display this in your templates.
You can use a location search parameter, which will be geocoded or directly provide latitude and longitude values:
$query = new WP_Query(array(
"geo_query" => array(
"location" => "London"
)
))

$query = new WP_Query(array(
"geo_query" => array(
"latitude" => -52.005,
"longitude" => 0.005,
)
))

Optionally, you can then filter by search radius.
By default, distances are given in miles. You can provide "units" => "km" if you need kilometres.
$query = new WP_Query(array(
"geo_query" => array(
"latitude" => -52.005,
"longitude" => 0.005,
"radius" => 10
)
))

Or order by nearness:
$query = new WP_Query(array(
"geo_query" => array(
"latitude" => -52.005,
"longitude" => 0.005
),
"orderby" => "geo"
))

Displaying distance in templates
In a WP_Query loop that includes a geo_query, you can use two extra functions to show distance away:

jhgs_get_the_distance(object $post) – which returns a rounded integer for the distance away, similar to get_the_title()

jhgs_the_distance(string $less_than_one, string $one, string $more_than_one) – which displays an approximate human-readable string, similar to the_title()
jhgs_the_distance will show one of three messages depending on whether the rounded distance is less than one, one, or greater than one. By default these are:

“Less than a mile away”

“About a mile away”
“About %s miles away”

If you need to use different units or translations, can pass three printf-formatted strings to jhgs_the_distance() to override these messages. Put %s where you want the value.
If you need the exact, unrounded value, you can use $post->distance.
Geocoding
Nominatim‘s service is used for geocoding location searches.
Using it is subject to an acceptable use policy – if you use case will involve lots of API calls, you should replace it with a paid alternative, like Google‘s.
📍 Populating latitude and longitude data
It looks for two custom field values with the keys latitude and longitude on your posts.
It’s agnostic about how you supply this data. The simplest thing to do is type it in using WordPress’s built-in custom field editor.
You could also hook into the save_post action to populate meta whenever you create or change a post, by adding a snippet like this to your theme’s functions.php:
function example_update_latlngs($post){
$location = get_field("location", $post);
if(isset($location)){
update_post_meta($post, "longitude", $location["lng"]);
update_post_meta($post, "latitude", $location["lat"]);
}
}

add_action("save_post", "example_update_latlngs", 10, 3);

This example assumes you are using an ACF Google Map field called “location”, but the data could come from anywhere, including a custom meta box you code yourself, so long as the post meta keys are right.
Bulk-updating existing posts
If you have many posts that you need to add longitude and latitude meta to in bulk, you could add something like this to functions.php, which will run on theme activation:
function example_update_all_latlngs(){
$query = new WP_Query(array(
"posts_per_page" => -1
));
foreach($query->get_posts() as $post){
// Function from above
example_update_latlngs($post);
}
}

add_action('after_switch_theme', 'example_update_all_latlngs');

各版本下載點

  • 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
  • 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「WP Geo search」來進行安裝。

(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。


最新版本

延伸相關外掛(你可能也想知道)

  • Car Route Planner Plugin 》路線規劃師可以計算世界各地汽車路線的不同數據,例如路線長度、行車時間、燃料量和費用等。, 主要用途:, , 為駕車者提供路線規劃(在自己或租用的汽車上進...。
  • Distance Rate Shipping For WooCommerce 》本外掛為 Woocommerce Distance Rate Shipping,可根據商店與客戶之間的距離計算運費。, 主要功能, , 與 WooCommerce 距離基礎運費一起,可為運送區域添加無...。
  • BP Distance Search 》BP Distance Search 新增了一種名為 Google Place Autocomplete 的欄位類型至你的 BuddyPress 擴展個人檔案,並將此欄位類型註冊到 BP Profile Search。, 安...。
  • Distance Based Shipping Calculator 》這個 Woocommerce 運費計算外掛是一個「距離 x 費率計算」工具。如果你正在尋找一個能夠即時查詢運輸商議價費率的外掛,請查看我們的完整 [Woocommerce 外掛...。
  • WP Find Your Nearest 》您是否曾經希望能讓訪客使用功能以搜尋他們最近的…商店、活動中心等等?, 這個外掛可以滿足您的需求。您可以建立任意數量的條目,利用 Google 地圖 API...。
  • WP GeoPosts 》特色, , 為任何內容類型添加 位置、緯度 和 經度 元數據和元框。, 提供了一個易於使用的界面,可以選擇哪些內容類型應用上述元數據。注意:此功能可選擇內置...。
  • WordPress Post Distance Filter 》從特定位置篩選文章。, 此外掛提供簡單的 WordPress 文章功能,當偵測到某些 URL 參數時,可以通過距離一個特定位置的距離排序存檔。您可以顯示文章的距離,...。
  • Store Manager 》使用此外掛程式,您可以管理多個商店(或任何類型的場所)。, 以下是支援的功能:, , 地址資訊, 聯絡資訊, 營業時間, 簡短描述, 詳細描述, 相片庫, 距離計算...。
  • Devinlabs Unit Conventer 》這個小工具是用來計算長度和距離轉換,包括公分、英呎、英吋、公里、米、英哩、毫米、碼。。
  • Ship Distance 》**總結:**, Ship Distance 是一個強大的外掛,允許您限制運送至店鋪位置特定距離內的地址。主要功能包括:, , - 使用經緯度設置最大送貨距離, - 自訂超出送貨...。

文章
Filter
Apply Filters
Mastodon