{"id":2343,"date":"2026-10-20T08:32:18","date_gmt":"2026-10-20T07:32:18","guid":{"rendered":"https:\/\/wpgeodirectory.com\/documentation\/article\/query-geodirectory-listings-by-field\/"},"modified":"2026-10-20T08:39:39","modified_gmt":"2026-10-20T07:39:39","slug":"query-geodirectory-listings-by-field","status":"publish","type":"gd_place","link":"https:\/\/wpgeodirectory.com\/documentation\/article\/api\/query-geodirectory-listings-by-field\/","title":{"rendered":"How to query GeoDirectory listings by city, zip or other fields"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A <code>WP_Query<\/code> with a <code>meta_query<\/code> won&#8217;t find GeoDirectory listings by address or custom fields, because GeoDirectory doesn&#8217;t store them as post meta. Each post type has its own table, such as <code>wp_geodir_gd_place_detail<\/code>, with one row per listing and one column per field (<code>city<\/code>, <code>region<\/code>, <code>zip<\/code>, your custom fields and so on).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Read fields for one listing<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">On a listing page the global <code>$gd_post<\/code> already holds every field. For any other listing, use the helper functions:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">global $gd_post;\n$zip = $gd_post-&gt;zip;\n\n\/\/ Or for a specific listing ID.\n$zip  = geodir_get_post_meta( $post_id, &#039;zip&#039;, true );\n$info = geodir_get_post_info( $post_id ); \/\/ Object with all fields.<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Find listings that match a field<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Join the posts table to the detail table. <code>geodir_db_cpt_table()<\/code> returns the right table name for a post type:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">global $wpdb;\n\n$table = geodir_db_cpt_table( &#039;gd_place&#039; );\n\n$count = (int) $wpdb-&gt;get_var(\n\t$wpdb-&gt;prepare(\n\t\t&quot;SELECT COUNT(p.ID) FROM {$wpdb-&gt;posts} p\n\t\tINNER JOIN {$table} d ON d.post_id = p.ID\n\t\tWHERE p.post_type = %s AND p.post_status = &#039;publish&#039; AND d.zip = %s&quot;,\n\t\t&#039;gd_place&#039;,\n\t\t&#039;11949&#039;\n\t)\n);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Swap <code>d.zip<\/code> for <code>d.city<\/code>, <code>d.region<\/code> or the key of any custom field, and use <code>SELECT p.ID<\/code> with <code>$wpdb-&gt;get_col()<\/code> when you want the IDs instead of a count.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Show the results in the usual listings layout<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You don&#8217;t need to write your own HTML. Pass the IDs to the GD &gt; Listings shortcode with <code>post_ids<\/code>. This example adds a <code>&#091;my_same_zip_places&#093;<\/code> shortcode that you can place on the Details page template to list other places with the same zip code:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">add_shortcode( &#039;my_same_zip_places&#039;, function () {\n\tglobal $wpdb, $gd_post;\n\n\tif ( empty( $gd_post-&gt;zip ) ) {\n\t\treturn &#039;&#039;;\n\t}\n\n\t$table = geodir_db_cpt_table( &#039;gd_place&#039; );\n\n\t$ids = $wpdb-&gt;get_col(\n\t\t$wpdb-&gt;prepare(\n\t\t\t&quot;SELECT p.ID FROM {$wpdb-&gt;posts} p\n\t\t\tINNER JOIN {$table} d ON d.post_id = p.ID\n\t\t\tWHERE p.post_type = &#039;gd_place&#039; AND p.post_status = &#039;publish&#039;\n\t\t\tAND d.zip = %s AND p.ID != %d&quot;,\n\t\t\t$gd_post-&gt;zip,\n\t\t\t$gd_post-&gt;ID\n\t\t)\n\t);\n\n\tif ( empty( $ids ) ) {\n\t\treturn &#039;&#039;;\n\t}\n\n\t$ids = implode( &#039;,&#039;, array_map( &#039;absint&#039;, $ids ) );\n\n\treturn do_shortcode( &#039;&#091;gd_listings post_type=&quot;gd_place&quot; post_limit=&quot;20&quot; post_ids=&quot;&#039; . $ids . &#039;&quot;&#093;&#039; );\n} );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you only need the standard filters (category, tags, author, featured and so on), the <a href=\"https:\/\/wpgeodirectory.com\/documentation\/article\/core-widgets\/gd-listings\/\">GD &gt; Listings<\/a> widget can do it without any code. For remote access there&#8217;s also the <a href=\"https:\/\/wpgeodirectory.com\/documentation\/article\/api\/endpoints\/\">GeoDirectory REST API<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A WP_Query with a meta_query won&#8217;t find GeoDirectory listings by address or custom fields, because GeoDirectory doesn&#8217;t store them as post meta. Each post type has its own table, such as wp_geodir_gd_place_detail, with one row per listing and one column per field (city, region, zip, your custom fields and so on). Read fields for one [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"article\/tags":[],"article\/categories":[882,883],"class_list":["post-2343","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\/2343","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=2343"}],"version-history":[{"count":2,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/2343\/revisions"}],"predecessor-version":[{"id":2611,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/2343\/revisions\/2611"}],"wp:attachment":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/media?parent=2343"}],"wp:term":[{"taxonomy":"gd_place_tags","embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/tags?post=2343"},{"taxonomy":"gd_placecategory","embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/categories?post=2343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}