A WP_Query with a meta_query won’t find GeoDirectory listings by address or custom fields, because GeoDirectory doesn’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 listing
On a listing page the global $gd_post already holds every field. For any other listing, use the helper functions:
global $gd_post;
$zip = $gd_post->zip;
// Or for a specific listing ID.
$zip = geodir_get_post_meta( $post_id, 'zip', true );
$info = geodir_get_post_info( $post_id ); // Object with all fields.
Find listings that match a field
Join the posts table to the detail table. geodir_db_cpt_table() returns the right table name for a post type:
global $wpdb;
$table = geodir_db_cpt_table( 'gd_place' );
$count = (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(p.ID) FROM {$wpdb->posts} p
INNER JOIN {$table} d ON d.post_id = p.ID
WHERE p.post_type = %s AND p.post_status = 'publish' AND d.zip = %s",
'gd_place',
'11949'
)
);
Swap d.zip for d.city, d.region or the key of any custom field, and use SELECT p.ID with $wpdb->get_col() when you want the IDs instead of a count.
Show the results in the usual listings layout
You don’t need to write your own HTML. Pass the IDs to the GD > Listings shortcode with post_ids. This example adds a [my_same_zip_places] shortcode that you can place on the Details page template to list other places with the same zip code:
add_shortcode( 'my_same_zip_places', function () {
global $wpdb, $gd_post;
if ( empty( $gd_post->zip ) ) {
return '';
}
$table = geodir_db_cpt_table( 'gd_place' );
$ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT p.ID FROM {$wpdb->posts} p
INNER JOIN {$table} d ON d.post_id = p.ID
WHERE p.post_type = 'gd_place' AND p.post_status = 'publish'
AND d.zip = %s AND p.ID != %d",
$gd_post->zip,
$gd_post->ID
)
);
if ( empty( $ids ) ) {
return '';
}
$ids = implode( ',', array_map( 'absint', $ids ) );
return do_shortcode( '[gd_listings post_type="gd_place" post_limit="20" post_ids="' . $ids . '"]' );
} );
If you only need the standard filters (category, tags, author, featured and so on), the GD > Listings widget can do it without any code. For remote access there’s also the GeoDirectory REST API.