Add zip code search

User
How to add search field for zip code in geoDirectory plugin search form
Currently there is no option to add zip code in search form
Assistant
To add a search field for zip code in the GeoDirectory plugin's search form, you'll need to customize the search form template or use a custom filter. Here's how you can do this:

### Method 1: Customize the Search Form Template
GeoDirectory provides template overrides, allowing you to modify the search form template.

1. **Create a Child Theme (if you don't already have one)**: It's a good practice to make customizations in a child theme to avoid losing changes during updates.

2. **Override the Search Form Template**:
– Navigate to your theme folder (or child theme folder) and create a new folder named `geodirectory`.
– Inside the `geodirectory` folder, create another folder called `geodirectory/search`.
– Now, copy the `search-form.php` file from the plugin's directory into your theme’s `geodirectory/search/` folder. The plugin’s original search form file is typically located in:

wp-content/plugins/geodirectory/templates/search/search-form.php

– Paste this file into your theme’s `geodirectory/search/` folder.

3. **Modify the Search Form**:
– Open the `search-form.php` file in your theme.
– Add an input field for the zip code where you want it in the search form:

<div class="geodir-search-field">
         <label for="geodir_zip_code"><?php _e('Zip Code', 'geodirectory'); ?></label>
         <input type="text" name="geodir_zip_code" id="geodir_zip_code" value="<?php echo isset($_GET['geodir_zip_code']) ? esc_attr($_GET['geodir_zip_code']) : ''; ?>" placeholder="<?php _e('Enter Zip Code', 'geodirectory'); ?>">
     </div>

4. **Handle the Zip Code in the Query**:
– You'll need to modify the search query to include the zip code. You can do this by hooking into the GeoDirectory query filter. Add the following code to your theme’s `functions.php` file:

function custom_geodir_zip_code_filter($query_args) {
         if ( isset($_GET['geodir_zip_code']) && !empty($_GET['geodir_zip_code']) ) {
             $zip_code = sanitize_text_field($_GET['geodir_zip_code']);
             // Modify the query to filter by zip code
             $query_args['meta_query'][] = array(
                 'key'     => 'geodir_zip_code', // Replace with the correct custom field key for zip code
                 'value'   => $zip_code,
                 'compare' => 'LIKE',
             );
         }
         return $query_args;
     }
     add_filter('geodir_search_query', 'custom_geodir_zip_code_filter');

– Make sure that `'geodir_zip_code'` corresponds to the correct meta key used by GeoDirectory for zip codes. If necessary, you can inspect the database to find the exact key.

5. **Test the Search**:
– After making these changes, you should see a zip code search field on the front-end of your site.
– When a user enters a zip code and submits the form, the search results should be filtered based on the entered zip code.

### Method 2: Use a GeoDirectory Addon
GeoDirectory also offers various add-ons that enhance search functionality, including advanced location search. You could explore the GeoDirectory add-ons to see if one of them provides a zip code search feature without the need for custom coding.

By following these steps, you should be able to successfully add a zip code search field to your GeoDirectory search form. Let me know if you need further assistance!

User
add zip code search field by hook
Assistant
To add a Zip Code search field to the GeoDirectory search form using hooks, you can use GeoDirectory’s built-in hooks to insert a custom search field and modify the search query. Here's how you can achieve this:

### Step 1: Add the Zip Code Search Field via Hook

GeoDirectory allows you to add custom fields to the search form using the `geodir_search_form` hook. In this step, you will add an input field for the Zip Code in the search form.

1. **Open your Theme's `functions.php` File**:
Add the following code to insert the Zip Code field in the search form.

function add_zip_code_search_field($form_html) {
    // Add Zip Code input field to the search form HTML
    $zip_code_field = '<div class="geodir-search-field">
                         <label for="geodir_zip_code">' . __('Zip Code', 'geodirectory') . '</label>
                         <input type="text" name="geodir_zip_code" id="geodir_zip_code" value="' . ( isset( $_GET['geodir_zip_code'] ) ? esc_attr( $_GET['geodir_zip_code'] ) : '' ) . '" placeholder="' . __('Enter Zip Code', 'geodirectory') . '">
                       </div>';

    // Append the Zip Code field to the search form
    return $form_html . $zip_code_field;
}
add_filter('geodir_search_form', 'add_zip_code_search_field');

This will add a Zip Code field to the search form using the `geodir_search_form` hook.

### Step 2: Modify the Search Query to Include Zip Code

Next, you need to modify the search query so that it filters results based on the zip code entered by the user. You can do this by using the `geodir_search_query` hook.

1. **Modify the Query in `functions.php`**:
Add the following code to modify the search query to take the zip code into account:

function custom_zip_code_filter_query($query_args) {
    if ( isset($_GET['geodir_zip_code']) && !empty($_GET['geodir_zip_code']) ) {
        $zip_code = sanitize_text_field($_GET['geodir_zip_code']);
        
        // Modify the query to filter by the Zip Code (replace 'geodir_zip_code' with the correct custom field key)
        $query_args['meta_query'][] = array(
            'key'     => 'geodir_zip_code', // The meta key where the zip code is stored
            'value'   => $zip_code,
            'compare' => 'LIKE',
        );
    }
    return $query_args;
}
add_filter('geodir_search_query', 'custom_zip_code_filter_query');

### Explanation:
– **`add_zip_code_search_field`**: This function uses the `geodir_search_form` filter to add an input field for the zip code in the search form.
– **`custom_zip_code_filter_query`**: This function uses the `geodir_search_query` filter to modify the search query based on the zip code entered. It adds a `meta_query` to filter the search results by the zip code.

### Step 3: Test the Search
Once you’ve added the above code to your `functions.php` file, you should:
1. See the Zip Code input field in the GeoDirectory search form.
2. Be able to filter search results based on the zip code entered.

### Notes:
– **Meta Key**: Ensure that `'geodir_zip_code'` corresponds to the correct custom field/meta key for zip code in your GeoDirectory setup. If you're unsure about the meta key, you can inspect the database or use a plugin like *Query Monitor* to check which meta keys are being used.
– **Validation**: You may want to add additional validation or sanitization depending on your requirements.

Let me know if you need further assistance!

User
geodir_search_form not adding
Assistant
If the `geodir_search_form` filter is not working as expected, it could be due to the context in which the search form is being rendered or because the hook is not triggering as intended. Here's how you can troubleshoot and correctly add the zip code field using the right approach:

### Step 1: Use the Correct Hook for GeoDirectory Search Forms

GeoDirectory uses several hooks and filters, and sometimes the search form might be rendered in a different context where custom hooks don’t fire. You should use the hook that specifically targets the search form rendering.

You can try using the `geodir_search_field_add` hook to add a custom field to the search form.

### Step 2: Correct Hook to Add the Zip Code Field

Try adding the zip code search field using the `geodir_search_field_add` hook, which is the more appropriate hook to add fields in GeoDirectory's search form.

function add_zip_code_search_field() {
    ?>
    <div class="geodir-search-field">
        <label for="geodir_zip_code"><?php _e('Zip Code', 'geodirectory'); ?></label>
        <input type="text" name="geodir_zip_code" id="geodir_zip_code" value="<?php echo isset($_GET['geodir_zip_code']) ? esc_attr($_GET['geodir_zip_code']) : ''; ?>" placeholder="<?php _e('Enter Zip Code', 'geodirectory'); ?>">
    </div>
    <?php
}
add_action('geodir_search_field_add', 'add_zip_code_search_field');

### Step 3: Modify the Query Based on the Zip Code Field

Now that you've added the zip code field to the search form, you need to modify the query to include the entered zip code. You can use the `geodir_search_query` filter to modify the search query based on the zip code.

function custom_zip_code_filter_query($query_args) {
    if ( isset($_GET['geodir_zip_code']) && !empty($_GET['geodir_zip_code']) ) {
        $zip_code = sanitize_text_field($_GET['geodir_zip_code']);
        
        // Modify the query to filter by zip code (replace 'geodir_zip_code' with the correct custom field key)
        $query_args['meta_query'][] = array(
            'key'     => 'geodir_zip_code', // The meta key where the zip code is stored
            'value'   => $zip_code,
            'compare' => 'LIKE',
        );
    }
    return $query_args;
}
add_filter('geodir_search_query', 'custom_zip_code_filter_query');

### Explanation:
1. **`geodir_search_field_add`**: This hook is specifically used to add custom fields to the GeoDirectory search form. By using this, you ensure the Zip Code field is correctly added to the form.
2. **`geodir_search_query`**: This filter modifies the search query to include the zip code entered by the user. This assumes the zip code is stored in a custom field with the key `geodir_zip_code`. You may need to adjust this key to match your actual field.

### Step 4: Test the Search
1. Visit the page where the GeoDirectory search form is located.
2. The zip code input field should now be visible.
3. Enter a zip code and submit the form. The search results should be filtered based on the zip code entered.

### Troubleshooting Tips:
– **Check for Errors**: Use the browser's Developer Tools to check if the zip code field is being rendered in the HTML.
– **GeoDirectory Debugging**: You can enable debugging in GeoDirectory to ensure that the hooks are firing as expected and the query is being modified properly.
– **Meta Key**: Double-check the custom field/meta key for the zip code in your GeoDirectory listings. You can do this by inspecting the database or checking the listing data in the admin area.

Let me know if this resolves the issue!

Leave a Comment

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