Skip Navigation

[Resolved] Sort options by nearest distance from submitted address

This support ticket is created 6 years, 1 month 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.

This topic contains 1 reply, has 2 voices.

Last updated by Beda 6 years, 1 month ago.

Author
Posts
#1121661

Hello,

On my website I have a page where a user can enter an address, hit submit, and (using the Toolset plugin) returns a number of locations that are within a certain mile radius. I am curious if there is a way that I can sort these locations by distance from the address that was submitted.

I have read this documentation - https://toolset.com/documentation/user-guides/display-on-google-maps/displaying-markers-on-google-maps/#filtering-markers-by-distance but I don't want to sort by distance from a fixed location or geolocation based on the user's IP. I would like to sort locations by distance based on the address that was submitted.

Is there a viable way to achieve what I'm asking for? Thanks.

#1121941

That is not possible.

When you order by, you can do two things:
Order by a preset data set and it's valued in the View's Backend.
This will apply always when you display the view and cannot be changed from the front end.

Or, you can add Sort and Orderby Controls to the Front-end, but those will always sort and order by something you first need to set. Which again are data sets with giving values. And, distance sort is not available in the front end.
This would require a new Feature for front-end sorting by distance.

Where instead to current user location or given fixed location, you could order by the location (distance) that was used as a search query.

You can, however, manipulate the View's settings when you submit a search with this hook
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_view_settings
In that hook, you could listen to the search location submitted (from the search params or query)
Then, you'd update the View's settings with that location.

This should be a working code, you'll need to adapt it a but to your case:

add_filter( 'wpv_view_settings', 'prefix_modify_rendered_view', 30, 2 );
function prefix_modify_rendered_view( $view_settings, $view_id ) {
    if ( $view_id == 51 ) { // if displaying a View with ID equal to 23
    	if ($_GET['toolset_maps_distance_center'] != '') {
    		$query_center = $_GET['toolset_maps_distance_center'];
    		$view_settings['distance_order'] = array(
	    		"source" => "fixed",
	      		"center" => $query_center
	    	);
    	}
    return $view_settings;
}
}

As you can see in the code, we filter the Views settings only if the query (front-end search) URL parameter for the distance from is not empty.
If so, we get that value and update the Views order by (order by distance from value)
So if a user visits the page we'll see the order you have set in the backend of the view.
As soon the user searches for any place as a center, the orderby will be by that center.