I want to add "Country" filter in search form

I want to add the “countries” filter as the first field in my search form. Right now, it’s showing up as the second field. Could you please tell me which hook I can use to set its priority to 1 so that it appears first?

Also, the “country” dropdown has more than 8000 entries, which is causing the page to load very slowly. I don’t want to load all the data at once. Instead, I’d like to load data dynamically via AJAX, so that when a user types/searches, the relevant data is fetched and displayed. Please guide me on how to achieve this.

Hi,

Please use this code snippet [How to add custom code snippets - HivePress Help Center]

add_filter('hivepress/v1/forms/listing_search', function ($form) {
 $form['fields']['field_name_here']['_order'] = 123;

    return $form;
}, 1000);

Regarding your second request, please note that there is no simple solution. We plan to improve this in the future, but for now, please consider using the Geolocation extension. By enabling the Regions feature, the search will also work for countries without the issues you described. Since you plan to use this dropdown not only for countries but also for more precise location searches, we recommend using the Geolocation plugin.

Hope I helped with your request

If I need to do some custom work for this country using AJAX, how can I achieve it? I want to override the code in the child theme and add custom code specifically for the country. Is this possible?

It’s possible, but requires a custom implementation. You can try adding the “source” parameter to the field configuration, then the drop-down will search results based on the REST API endpoint. The endpoint URL should return results in JSON format.

Please consider using Geolocation instead as this is the recommended way to add the location search to listings, this also adds other benefits like the radius-based search, and the free limits of map providers are enough unless there’s high traffic. Unfortunately we can’t advise on implementing a custom location search in detail.

1 Like

Can you please share a sample code (using a hook) for implementing this with “Country”?
I want to display an input field in the filter, and when a user searches, it should fetch data via AJAX and show the results in a dropdown.

Please check the example of using the “source” parameter here Data wont save when pulling values for select from API - #31 by swordswebdesign Please note that this also requires implementing a custom API endpoint under the URL set for the “source” parameter.

I created a custom country attribute where the current field name is country-2. Right now, no countries are added in this field, so it is blank. I want to populate this field dynamically using an API that fetches country data and then displays it in the dropdown.

However, instead of using the existing country-2 field, I would like to create a new attribute for this and handle it through custom code.

Currently, the ‘Country’ field is a selectable dropdown, but if I change it to a text input, the filter stops working. At the moment, since the field is a select box, it loads all 8000+ countries at once (which I added manually). Instead, I want to use a text input so I can dynamically load the country list via AJAX. However, as I mentioned, when I switch the field from a select to a text input, the filter doesn’t work. Please help me understand how to fix this
image|690x175

Hello,
I want to help you somehow with my dropdown plugin attempts. I’m not a coder so everything I did is based on what I read on the forum, ex. that post mentioned by ihor, if they are helpful to you use them as you wish. I realized that for my project that I want to do someday based on the request extension, I can just set the categories as car makes and the models as attributes. For the conditional dropdown I’m waiting for a version to be released, a professionally written extension. For the ones in the links (see here and also check here), there are limitations,it easily handles 1000-3000 rows, but I don’t think 8000 can, and for example if I set the models as filterable and type is select, the main page enters a continuous reload, I have to select text to be able to use the filtering on the model, for the makes work fine the select type.
What I wanted to achieve is a reading from the .csv file for easier information maintenance. My hardcoded attempts with the REST API did not lead to any results, I was unable to make the information appear in my listings… Have fun with them, modify them, maybe they will be useful to you, I hope these
codes help you

…ah, good tip, since coded attributes don’t use special characters in the country list or separate words, they won’t work as a filter. Connect words together with “_” or “-”

I have set the Country filter field as selectable, and in that case, it works fine.
But when I change it to a searchable text input, it doesn’t work. I want the filter to search by both country ID and country name, but it only works when the field type is selectable.

Why is it not working with the text input type? Please provide me with a solution. If it works with a text input, then I can implement a custom AJAX-based dropdown. But if it doesn’t work with input fields at all, then that seems wrong.

I changed the selectable field to a text input from the admin panel, and it is showing in the searchable filter. However, the filter is not working.

add_filter(
    'hivepress/v1/models/listing/attributes',
    function( $attributes) {
     
        if ( isset( $attributes['country'] ) ) {
            $attributes['country']['searchable'] = true;
            $attributes['country']['filter_type'] = 'text'; // ya 'autocomplete'
        }
    
        return $attributes;
    },
    1000
);

Sorry, we can only provide general developer guidance, unfortunately we can’t help with reviewing custom code in detail and implementing custom features as this is outside of our support scope. We plan to add the AJAX search feature to drop-down fields, but there’s no such option yet. The officially recommended solution for this issue is using Geolocation, then you can rely on the map provider API instead of creating a custom plain list of locations.

Please note that for the “source” parameter to work, the field type should be “select”, and this parameter should point to a URL that returns a list in JSON format similarly to how this function returns it (it’s a sample REST API endpoint for selecting listings in the drop-down) hivepress/includes/controllers/class-listing.php at master · hivepress/hivepress · GitHub

Hope this helps

Can you tell me in which file the query code for the search filter is located, so that I can override the query?

If you mean the listing search results query, you can try this hook (use “listing” as model name) Action: hivepress/v1/models/{model_name}/search | HivePress Hook Reference This way you can customize the WP_Query object during the listing search.

Hello there!!

Can this method be used to fetch dynamically (similar to AJAX behavior)
sub-attribute values that will populate a select box on => listings/category => toolbar ?

I’ve read what you provided but my use case differs from what mentioned earlier.
(i need to have 40k filtering options, on filtering form, not on search bar)
i am a bit new to coding and just want to make sure that it’s doable before touching anything

thank you

Yes, with 40k options creating a custom REST API endpoint is definitely needed, and providing its URL for the “source” parameter of the drop-down field. This way the whole list will not be rendered at once, instead it will be fetched via AJAX on typing.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.