Tell us what you are trying to do?
When some searches are done there are, of course, some times when there are no results found - example: 20 miles, Hull UK, Personal Policy, Health Insurance
When there are no results, I would like to show the NEAREST company who match the search (other than the distance radius), with the message: "There are no results in the area you specified. Your nearest company is" and then display that result.
Hello. Thank you for contacting the Toolset support.
There is no such AI feature available that system will automatically query nearest location.
The only possibility is you can display a fixed location when there is no results found as Views works on the supplied search arguments and simply if it will find the result it will display it and if not, there is nothing to display.
There is no automatic way to fire another query and system do not know what is the nearest location.
When the query is created the system is told:
Users location, users choice of distance radius, and the search criteria
Surely we can return an array of all results and then
a) loop through and list all results that are < the distance selected
b) if there are no results within the distance - show the first lowest distance result
That is what I'm saying that there is no automatic way to do so.
How to tell system that with what range you should display the result within how many km/miles. If there is result it will display the result within the specified address, if there is no result there is no way to run another query. This could be achieved but then it needs extensive custom programming and such custom work is beyond the scope of our support policy.
If you need such custom programming for your project, you're welcome to contact any of our certified partners:
- https://toolset.com/contractors/
Thanks. I don't see the need to run another query if the initial query returns ALL results at ANY distance. Then loops through the results to see if there are any results within the distance, if not it returns the first (nearest) result in the returned query.
Toolset view does not work like that way you think.
As shared, whatever address you passed to view, if the results found within the range (with applicable filters user added to his search) it will display the results otherwise not.
I already shown you how you can customize the results with the Toolset views hook "wpv_filter_query_post_process".
You can customize your output as per your requirement by adding your business logic there.