{"id":2943,"date":"2026-10-20T08:40:00","date_gmt":"2026-10-20T07:40:00","guid":{"rendered":"https:\/\/wpgeodirectory.com\/documentation\/article\/query-listings-by-distance-2\/"},"modified":"2026-10-20T08:40:00","modified_gmt":"2026-10-20T07:40:00","slug":"query-listings-by-distance-2","status":"publish","type":"gd_place","link":"https:\/\/wpgeodirectory.com\/documentation\/article\/api\/query-listings-by-distance-2\/","title":{"rendered":"How to query GeoDirectory listings by distance"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A plain <code>WP_Query<\/code> cannot sort GeoDirectory listings by distance, because the coordinates are stored in the GeoDirectory detail table and not in post meta. Use one of the options below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Listings near the one being viewed<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To show nearby listings on a listing page, no code is needed. Add the GD > Listings block to your details template page and set Sort by to &#8220;Distance to current post (details page only)&#8221;. The block then lists other listings ordered by distance from the one being viewed:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-markup\">&#091;gd_listings post_type=&quot;gd_place&quot; sort_by=&quot;distance_asc&quot; post_limit=&quot;5&quot;&#093;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Combine it with &#8220;Filter listings related to&#8221; if you only want nearby listings from the same category. To show how far each one is, add <a href=\"https:\/\/wpgeodirectory.com\/documentation\/article\/core-widgets\/gd-distance-to-post\/\">GD > Distance to Post<\/a> to your archive item template.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Listings near any point<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The search page already does this. A search URL with coordinates returns listings ordered nearest first, for example:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-markup\">https:\/\/example.com\/search\/?geodir_search=1&amp;stype=gd_place&amp;s=&amp;snear=&amp;sgeo_lat=51.5072&amp;sgeo_lon=-0.1276<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Use the address of your own search page. Results are limited to the &#8220;Search near radius&#8221; set in GeoDirectory > Settings > General > Search, unless you add a <code>dist<\/code> parameter. If you start from a postcode, geocode it to latitude and longitude first, then pass those values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the REST API<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For your own code or an app, the markers endpoint returns listings within a radius of a point:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-markup\">https:\/\/example.com\/wp-json\/geodir\/v2\/markers\/?post_type=gd_place&amp;lat=51.5072&amp;lon=-0.1276&amp;dist=10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>dist<\/code> is the radius in the unit set in GeoDirectory > Settings > General > Search (Search distances, miles or kilometers). Without it, the &#8220;Search near radius&#8221; setting is used. Each result has the listing ID (<code>m<\/code>), title (<code>t<\/code>) and coordinates (<code>lt<\/code>, <code>ln<\/code>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The endpoint does not sort by distance by default. This snippet orders the results nearest first whenever coordinates are passed:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">add_filter( &#039;geodir_rest_markers_query_order_by&#039;, function( $order_by, $request ) {\n\tif ( ! empty( $request&#091;&#039;lat&#039;&#093; ) &amp;&amp; ! empty( $request&#091;&#039;lon&#039;&#093; ) ) {\n\t\t$order_by = &#039;distance ASC&#039;;\n\t}\n\n\treturn $order_by;\n}, 10, 2 );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">See the <a href=\"https:\/\/wpgeodirectory.com\/documentation\/article\/api\/endpoints\/\">API endpoints<\/a> for the rest of the REST API.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A plain WP_Query cannot sort GeoDirectory listings by distance, because the coordinates are stored in the GeoDirectory detail table and not in post meta. Use one of the options below. Listings near the one being viewed To show nearby listings on a listing page, no code is needed. Add the GD > Listings block to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"closed","template":"","meta":{"footnotes":""},"article\/tags":[],"article\/categories":[882,883],"class_list":["post-2943","gd_place","type-gd_place","status-publish","hentry","gd_placecategory-api","gd_placecategory-developer-docs"],"_links":{"self":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/2943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article"}],"about":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/types\/gd_place"}],"author":[{"embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/comments?post=2943"}],"version-history":[{"count":0,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/2943\/revisions"}],"wp:attachment":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/media?parent=2943"}],"wp:term":[{"taxonomy":"gd_place_tags","embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/tags?post=2943"},{"taxonomy":"gd_placecategory","embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/categories?post=2943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}