{"id":131740,"date":"2020-12-13T23:59:03","date_gmt":"2020-12-13T23:59:03","guid":{"rendered":"https:\/\/wordpress.org\/plugins\/wp-geo-search\/"},"modified":"2021-09-06T13:29:12","modified_gmt":"2021-09-06T13:29:12","slug":"wp-geo-search","status":"publish","type":"plugin","link":"https:\/\/pap-aw.wordpress.org\/plugins\/wp-geo-search\/","author":15614609,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_crdt_document":"","version":"0.1","stable_tag":"trunk","tested":"5.8.0","requires":"4.0","requires_php":"7.0","requires_plugins":"","header_name":"WP Geo Search","header_author":"Jaye Hackett","header_description":"Add a location-aware geographic search to WP_Query for use in your templates","assets_banners_color":"979d9f","last_updated":"2021-09-06 13:29:12","external_support_url":"","external_repository_url":"","donate_link":"https:\/\/github.com\/jhackett1\/wp-geo-search","header_plugin_uri":"https:\/\/github.com\/jhackett1\/wp-geo-search","header_author_uri":"https:\/\/jayehackett.com","rating":5,"author_block_rating":0,"active_installs":70,"downloads":1591,"num_ratings":2,"support_threads":0,"support_threads_resolved":0,"author_block_count":0,"sections":["description","faq","changelog"],"tags":[],"upgrade_notice":[],"ratings":{"1":0,"2":0,"3":0,"4":0,"5":2},"assets_icons":{"icon-128x128.png":{"filename":"icon-128x128.png","revision":2438555,"resolution":"128x128","location":"assets","locale":""},"icon-256x256.png":{"filename":"icon-256x256.png","revision":2438555,"resolution":"256x256","location":"assets","locale":""}},"assets_banners":{"banner-1544x500.jpg":{"filename":"banner-1544x500.jpg","revision":2438555,"resolution":"1544x500","location":"assets","locale":""},"banner-772x250.jpg":{"filename":"banner-772x250.jpg","revision":2438555,"resolution":"772x250","location":"assets","locale":""}},"assets_blueprints":{},"all_blocks":[],"tagged_versions":[],"block_files":[],"assets_screenshots":[],"screenshots":[],"jetpack_post_was_ever_published":false},"plugin_section":[],"plugin_tags":[5518,2591,194048,2215,194047],"plugin_category":[49],"plugin_contributors":[194049],"plugin_business_model":[],"class_list":["post-131740","plugin","type-plugin","status-publish","hentry","plugin_tags-distance","plugin_tags-geo","plugin_tags-haversine","plugin_tags-location","plugin_tags-nominatim","plugin_category-maps-and-location","plugin_contributors-jhackett1","plugin_committers-jhackett1"],"banners":{"banner":"https:\/\/ps.w.org\/wp-geo-search\/assets\/banner-772x250.jpg?rev=2438555","banner_2x":"https:\/\/ps.w.org\/wp-geo-search\/assets\/banner-1544x500.jpg?rev=2438555","banner_rtl":false,"banner_2x_rtl":false},"icons":{"svg":false,"icon":"https:\/\/ps.w.org\/wp-geo-search\/assets\/icon-128x128.png?rev=2438555","icon_2x":"https:\/\/ps.w.org\/wp-geo-search\/assets\/icon-256x256.png?rev=2438555","generated":false},"screenshots":[],"raw_content":"<!--section=description-->\n<p>A plugin to add location-aware geographical search to <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/\">WP_Query<\/a>.<\/p>\n\n<p>You can use it to power location-aware apps, such as showing a user results near them.<\/p>\n\n<h3>\ud83d\udd0e Using it in a query<\/h3>\n\n<p>Adding a <code>geo_query<\/code> parameter to WP_Query will add a \"distance\" column to the returned results, provided they have the right metadata.<\/p>\n\n<p>You can then display this in your templates.<\/p>\n\n<p>You can use a location search parameter, which will be <a href=\"#geocoding\">geocoded<\/a> or directly provide latitude and longitude values:<\/p>\n\n<pre><code>$query = new WP_Query(array(\n    \"geo_query\" =&gt; array(\n            \"location\" =&gt; \"London\"\n    )\n))\n\n$query = new WP_Query(array(\n    \"geo_query\" =&gt; array(\n            \"latitude\" =&gt; -52.005,\n            \"longitude\" =&gt; 0.005,\n    )\n))\n<\/code><\/pre>\n\n<p>Optionally, you can then filter by search radius.<\/p>\n\n<p>By default, distances are given in miles. You can provide <code>\"units\" =&gt; \"km\"<\/code> if you need kilometres.<\/p>\n\n<pre><code>$query = new WP_Query(array(\n    \"geo_query\" =&gt; array(\n            \"latitude\" =&gt; -52.005,\n            \"longitude\" =&gt; 0.005,\n            \"radius\" =&gt; 10\n    )\n))\n<\/code><\/pre>\n\n<p>Or order by nearness:<\/p>\n\n<pre><code>$query = new WP_Query(array(\n    \"geo_query\" =&gt; array(\n            \"latitude\" =&gt; -52.005,\n            \"longitude\" =&gt; 0.005\n    ),\n    \"orderby\" =&gt; \"geo\"\n))\n<\/code><\/pre>\n\n<h3>Displaying distance in templates<\/h3>\n\n<p>In a <code>WP_Query<\/code> loop that includes a <code>geo_query<\/code>, you can use two extra functions to show distance away:<\/p>\n\n<ul>\n<li><code>jhgs_get_the_distance(object $post)<\/code> - which returns a rounded integer for the distance away, similar to <code>get_the_title()<\/code><\/li>\n<li><p><code>jhgs_the_distance(string $less_than_one, string $one, string $more_than_one)<\/code> - which displays an approximate human-readable string, similar to <code>the_title()<\/code><\/p>\n\n<p>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:<\/p><\/li>\n<li><p>\"Less than a mile away\"<\/p><\/li>\n<li>\"About a mile away\"<\/li>\n<li>\"About %s miles away\"<\/li>\n<\/ul>\n\n<p>If you need to use different units or translations, can pass three <a href=\"https:\/\/www.php.net\/manual\/en\/function.printf.php\">printf-formatted<\/a> strings to <code>jhgs_the_distance()<\/code> to override these messages. Put <code>%s<\/code> where you want the value.<\/p>\n\n<p>If you need the <em>exact<\/em>, unrounded value, you can use <code>$post-&gt;distance<\/code>.<\/p>\n\n<h3>Geocoding<\/h3>\n\n<p><a href=\"https:\/\/nominatim.org\/\">Nominatim<\/a>'s service is used for geocoding location searches.<\/p>\n\n<p>Using it is subject to an <a href=\"https:\/\/operations.osmfoundation.org\/policies\/nominatim\/\">acceptable use policy<\/a> - if you use case will involve lots of API calls, you should replace it with a paid alternative, like <a href=\"https:\/\/developers.google.com\/maps\/documentation\/geocoding\/overview\">Google<\/a>'s.<\/p>\n\n<h3>\ud83d\udccd Populating latitude and longitude data<\/h3>\n\n<p>It looks for two <a href=\"https:\/\/wordpress.org\/support\/article\/custom-fields\/\">custom field<\/a> values with the keys <code>latitude<\/code> and <code>longitude<\/code> on your posts.<\/p>\n\n<p>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.<\/p>\n\n<p>You could also hook into the <code>save_post<\/code> action to populate meta whenever you create or change a post, by adding a snippet like this to your theme's <code>functions.php<\/code>:<\/p>\n\n<pre><code>function example_update_latlngs($post){\n    $location = get_field(\"location\", $post);\n    if(isset($location)){\n        update_post_meta($post, \"longitude\", $location[\"lng\"]);\n        update_post_meta($post, \"latitude\", $location[\"lat\"]);\n    }\n}\n\nadd_action(\"save_post\", \"example_update_latlngs\", 10, 3);\n<\/code><\/pre>\n\n<p>This example assumes you are using an <a href=\"https:\/\/www.advancedcustomfields.com\/resources\/google-map\/\">ACF Google Map<\/a> 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.<\/p>\n\n<h3>Bulk-updating existing posts<\/h3>\n\n<p>If you have many posts that you need to add longitude and latitude meta to in bulk, you could add something like this to <code>functions.php<\/code>, which will run on <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/after_switch_theme\/\">theme activation<\/a>:<\/p>\n\n<pre><code>function example_update_all_latlngs(){\n    $query = new WP_Query(array(\n        \"posts_per_page\" =&gt; -1\n    ));\n    foreach($query-&gt;get_posts() as $post){\n        \/\/ Function from above\n        example_update_latlngs($post);\n    }\n}\n\nadd_action('after_switch_theme', 'example_update_all_latlngs');\n<\/code><\/pre>\n\n<!--section=faq-->\n<dl>\n<dt id='i%27ve%20activated%20it%2C%20but%20i%20can%27t%20see%20any%20changes%20to%20my%20website'><h3>I've activated it, but I can't see any changes to my website<\/h3><\/dt>\n<dd><p>This plugin adds extra functionality to <code>WP_Query<\/code> - it's up to you to make use of that functionality in your theme.<\/p><\/dd>\n\n<\/dl>\n\n<!--section=changelog-->\n<h4>0.1<\/h4>\n\n<ul>\n<li>First release<\/li>\n<\/ul>","raw_excerpt":"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.","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/pap-aw.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin\/131740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pap-aw.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin"}],"about":[{"href":"https:\/\/pap-aw.wordpress.org\/plugins\/wp-json\/wp\/v2\/types\/plugin"}],"replies":[{"embeddable":true,"href":"https:\/\/pap-aw.wordpress.org\/plugins\/wp-json\/wp\/v2\/comments?post=131740"}],"author":[{"embeddable":true,"href":"https:\/\/pap-aw.wordpress.org\/plugins\/wp-json\/wporg\/v1\/users\/jhackett1"}],"wp:attachment":[{"href":"https:\/\/pap-aw.wordpress.org\/plugins\/wp-json\/wp\/v2\/media?parent=131740"}],"wp:term":[{"taxonomy":"plugin_section","embeddable":true,"href":"https:\/\/pap-aw.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_section?post=131740"},{"taxonomy":"plugin_tags","embeddable":true,"href":"https:\/\/pap-aw.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_tags?post=131740"},{"taxonomy":"plugin_category","embeddable":true,"href":"https:\/\/pap-aw.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_category?post=131740"},{"taxonomy":"plugin_contributors","embeddable":true,"href":"https:\/\/pap-aw.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_contributors?post=131740"},{"taxonomy":"plugin_business_model","embeddable":true,"href":"https:\/\/pap-aw.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_business_model?post=131740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}