Skip Navigation

[Resolved] Remove location/distance filter programmatically

This support ticket is created 5 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 5 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1296813

I am looking to disable the location/distance filter from the code programmatically using certain conditions.

You can see what I am trying to achieve on the URL:
hidden link

Basically, when the location is updated in the field, I am checking whether it is a country or a state, or another type of location.

1. If it is country or state, I want the query to only do a custom field search using the state and country fields and ignore the location field.

2. If it is another location type (city, postcode, street, user's location), I want to so the default search with that location and a set distance of 30kms.

For this purpose, I have added two text fields which will populate the state and country if you search for a name of a state, province or country, for example "Ontario, Canada" or "Australia", or "Victoria, Australia". The fields are visible for testing purposes now but I will eventually hide them. These fields are also linked to "geo_state" and "geo_country" URL parameters.

I have two issues:

1. I already wrote JS for them to be populated when the location field is updated. Unfortunately, the fields appear and then are reset instantly. I applied some delay of "1400ms" but it only works on and off, so it is not reliable. You can check my JS code here:

https://pastebin.com/aATsHYJy

I am already saving the state and country in the custom field and the search works great whenever the fields get populated properly or if you manually enter state and country in those fields. Is there any way to prevent the state and country fields from being reset in the JS?

2. My second issue is that I want to unhook/remove the location/distance filter when the aforementioned state and country fields are being queried, i.e. when the "geo_country" URL param exists, I want the query to ignore the "toolset_maps_distance_center" and "toolset_maps_distance_radius" URL parameters and search as if the location field is empty. How do I do that programatically?

#1297049

These fields are also linked to "geo_state" and "geo_country" URL parameters.
Hi, I'm not sure exactly what this means. Can you explain in more detail? Do you have custom fields called "geo_state" and "geo_country"?

1. ...Is there any way to prevent the state and country fields from being reset in the JS?
Not exactly, but we do offer some front-end event hooks you can use to listen for the reset event. You can find these events in the View editor by going to the Search and Pagination editor panel and opening the JS panel. There's a button there you can use to insert the framework for any of several event hooks. Since it looks like your View uses the "Show only available options" feature, you may find the "search form updated" event useful. Here's an example:

jQuery( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) {
	/**
	* data.view_unique_id (string) The View unique ID hash
	* data.view_changed_form (object) The jQuery object for the View form after being updated
	* data.view_changed_form_additional_forms_only (object) The jQuery object containing additional forms from other instances of the same View inserted using the [wpv-form-view] shortcode
	* data.view_changed_form_additional_forms_full (object) The jQuery object containing additional forms from other instances of the same View inserted using the [wpv-view] shortcode
	*/
	console.log('search form updated');
});

You would probably have to store the geocoded state and country values in a more globally available variable, and then set the form elements again manually after the form is updated. This would get around the need for a timeout.

2. My second issue is that I want to unhook/remove the location/distance filter when the aforementioned state and country fields are being queried
The wpv_filter_query PHP API is the best way to manipulate the filter criteria sent to the WP search query. We have more information about this API available here: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
In your callback, you will be able to inspect the $_GET superglobal to determine which fields have provided values. Based on that information, you could manipulate the distance filter as needed.