GeoDirectory ZIP Code Search

User
geodirectory customize search form to include zip code search for placess
Assistant
To customize the **GeoDirectory** search form in WordPress to include a **ZIP code search** for places, you'll need to use the built-in GeoDirectory search functionalities and possibly customize it further using hooks or shortcodes. Here's how you can add a ZIP code search field to the GeoDirectory search form:

### Step 1: Add ZIP Code Field in GeoDirectory Settings

1. **Log in to your WordPress Dashboard**.
2. Navigate to **GeoDirectory** > **Places** > **Settings**.
3. Go to the **"Search" tab**.
4. Locate the **"Search Filters"** section.
5. **Enable "Zip/Post Code"** as a search field.
6. Save your settings.

### Step 2: Customize the GeoDirectory Search Form (If Needed)

If you want to customize the search form further, you can add the ZIP code field manually using a shortcode or template customization.

#### Method 1: Using Shortcodes
You can use the `[gd_search]` shortcode to customize the search form and include a ZIP code search. Add the following shortcode to any page or widget:

[gd_search post_types="gd_place" fields="keywords,city,region,zip,category"]

– **`post_types`**: Specifies the custom post type (e.g., `gd_place` for places).
– **`fields`**: Include fields like `keywords`, `city`, `region`, `zip`, `category`, etc.

#### Method 2: Modifying the Template (Advanced)

If you want more control over the search form's appearance, you can modify the template file.

1. **Copy the template file** from:
– `wp-content/plugins/geodirectory/templates/parts/search-form.php`

2. Paste it into your **child theme**:
– `wp-content/themes/your-child-theme/geodirectory/parts/search-form.php`

3. **Edit `search-form.php`** and add a custom ZIP code field:

<input type="text" name="zip" id="gd_search_zip" placeholder="Enter ZIP Code" value="<?php echo isset($_GET['zip']) ? esc_attr($_GET['zip']) : ''; ?>">

4. Save the file.

### Step 3: Use Hooks to Add Custom ZIP Code Handling (Optional)

If you want to process the ZIP code search in a custom way, you can use GeoDirectory hooks like `geodir_search_advanced_filters` or `geodir_search_query`.

Example of adding a custom filter:

add_filter('geodir_search_query', 'custom_zip_code_search', 10, 2);
function custom_zip_code_search($where, $params) {
    if (!empty($params['zip'])) {
        global $wpdb;
        $zip = esc_sql($params['zip']);
        $where .= " AND $wpdb->posts.ID IN (SELECT post_id FROM {$wpdb->prefix}geodir_gd_place_detail WHERE post_zip LIKE '%$zip%')";
    }
    return $where;
}

### Step 4: Testing

1. Go to the front-end page where you have added your search form.
2. Test the search functionality by entering a ZIP code and checking if it returns the correct results.

This setup allows you to enhance the search capabilities of GeoDirectory by including ZIP code searches, helping users find places more efficiently based on postal codes.

Leave a Comment

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