GeoDirectory Zip Code Search

User
Geodirectory gd_place search by zip code
Assistant
To search by zip code using GeoDirectory's `gd_place` functionality, you can follow these steps:

1. **Ensure that the zip code field is enabled**: GeoDirectory allows you to create custom fields, so make sure the zip code field is available in your place listings. If not, you can create one in the GeoDirectory settings.

2. **Using the GeoDirectory Place Search Form**: By default, the GeoDirectory search form should have location filters that include zip codes. You can enable or customize these search filters in the **GeoDirectory settings**.

3. **Custom Search Query by Zip Code**:
If you want to perform a custom query to search by zip code, you can use the GeoDirectory API or a custom WP_Query with the zip code parameter.

Here's an example of how you could modify or use the `gd_place` post type search:

// Assuming the 'zip_code' is stored as a custom field or taxonomy
   $args = array(
       'post_type' => 'gd_place',
       'posts_per_page' => -1, // Change this based on how many results you want
       'meta_query' => array(
           array(
               'key'     => 'zip_code', // The meta key for the zip code field
               'value'   => '12345',     // Replace with the zip code you're searching for
               'compare' => '=',         // Optional: set comparison operator if needed
           ),
       ),
   );

   $query = new WP_Query( $args );

   if ( $query->have_posts() ) {
       while ( $query->have_posts() ) {
           $query->the_post();
           // Output the place or additional fields here
       }
   } else {
       echo 'No places found for this zip code.';
   }

   wp_reset_postdata();

4. **Search by Zip Code on the Frontend**:
If you're using the GeoDirectory search form widget, ensure it's configured to allow users to filter by zip code. You may need to customize the form if the zip code field is not already present. You can either add a custom form or tweak the existing one.

Example of a custom form that allows users to enter a zip code:

<form action="" method="get">
       <input type="text" name="zip_code" placeholder="Enter Zip Code" value="<?php echo isset($_GET['zip_code']) ? esc_attr($_GET['zip_code']) : ''; ?>" />
       <button type="submit">Search</button>
   </form>

   <?php
   if (isset($_GET['zip_code'])) {
       $zip_code = sanitize_text_field($_GET['zip_code']);
       // Perform the custom query as shown above
   }
   ?>

5. **Customizing GeoDirectory's Search Results**:
If you want to customize the search results further, such as showing only places within a certain radius of the zip code, you may need to extend the default GeoDirectory search functionality. GeoDirectory provides hooks and filters that allow customization of search queries.

If you need more specific adjustments, feel free to provide more details about your setup.

Leave a Comment

Your email address will not be published. Required fields are marked *