Skip Navigation

[Resuelto] Display distance in search results or access visitor's location

This support ticket is created hace 6 años, 1 mes. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Este tema contiene 8 respuestas, tiene 2 mensajes.

Última actualización por Nigel hace 6 años, 1 mes.

Asistido por: Nigel.

Autor
Mensajes
#1119974

Tell us what you are trying to do?
I've seen mention of Maps adding a shortcode to get the visitor's location but I can't find the shortcode. Is it implemented?

We'd like to show "approximately nn miles" in each result when searching and sorting by distance in a dealer locator. It would be great if this was a built-in shortcode that leveraged cached data from the search but even if it is a shortcode or API that lets us get the lat / lng of the searched for location (which may or may not be the same as the current visitor's geolocation), we can do the math to show approximate distance for each result using the stored lat / lng from the results' address fields.

Is there any documentation that you are following?
Can't seem to find this in the documentation.

Is there a similar example that we can see?
We removed our home grown distance search which had this capability in favor of using Toolset Maps but all we're talking about is adding "approximately nn miles" to each search result (in listing or marker popup) as many large companies' store locators do. You can see an example in Walmart's Store Finder: enlace oculto

What is the link to your site?
enlace oculto

Separately, it would be really nice to be able to sort search results on different criteria such as title ascending until a search is performed then sort on distance from the searched location. Right now we either have to accept somewhat random ordering or put the view in a geolocation block which causes the browser to ask permission to access the visitor's location if we want to use distance sorting. We'd rather not force the permission question until / unless they click the button to use their location.

#1120638

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Hi Scott

This feature has been requested and accepted, and is currently being worked on for the next main Maps update to 1.6.

It should have the ability to show distance from the current user's location, or from a location specified in the search.

I'm not seeing the problem you report with ordering, so you might need to clarify.

If I order my map View results by distance from my location, and I include a distance filter, when I visit a page on the front end with this View, the results are not initially ordered by distance (I think it is probably by post date), but I only get asked for permission for the browser to use my location if I click that button in the distance filter, not before.

#1120770

Hi Nigel,

On the current site I removed the geolocation tags from around the view which is why you don't see the prompt for permission to access location until a search is done.

We'd really like to control how the list is sorted pre-search. By post-date doesn't make sense here. Ascending title would make much more sense.

Other than that, we can wait for 1.6 for the ability to show distance in the results list.

#1121547

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Hi Scott, I wasn't looking at your site, I was trying this on a test site.

I will check whether secondary sorting when using distance can be added in to the next Maps update as well, in the expectation that if no location had been set, the secondary sort option would be in effect.

Otherwise I think you could add a little code to check whether a distance filter had been set, and if not set the query arguments to orderby post title.

The queue is very busy at the moment so I don't have time to write such a solution, but it would involve using the wpv_filter_query hook and checking whether a distance filter has been applied, and if not updating the arguments for orderby.

Let me know if you are able to do that, otherwise I should have some time next week to help further.

#1121554

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Quick update: I spoke with the Maps developer who said because of the way distance searches are built it is not practicable to add secondary ordering when ordering by distance, so using the wpv_filter_query route is the way to go.

#1121576

Thanks Nigel. We don't need secondary sorting (it would only be useful as a tiebreaker in the event that two results were the same distance away). What we need is control over primary sorting prior to the distance search being executed if we have the results on the same page as the search form. Once a distance search is begun we only need distance sorting. Until then, we'd like to have ascending title sorting instead of descending post date.

it would also be useful to be able to put all results on the map but still paginate the listings below but I can see how that would be pretty difficult technically since it would need two loops -- maybe a view in a view where the criteria are passed through to the second view?

#1122682

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Hi Scott

I took a look at writing a code snippet to override the sort order when you are not yet searching by distance and came up with this, if you want to try it out:

<?php
/**
 * Modify orderby if not yet searching by location
 */
function tssupp_custom_orderby($query_vars, $view_settings, $view_id) {

	if (in_array($view_id, array(59)) && !isset($_REQUEST['toolset_maps_distance_radius'])) {

		$posts = $query_vars->posts;

		usort($posts, function ($a, $b) {
			return strcmp($a->post_title, $b->post_title);
		});

		$query_vars->posts = $posts;
	}

	return $query_vars;

}
add_filter('wpv_filter_query_post_process', 'tssupp_custom_orderby', 1001, 3);

Regarding paginating the list of results but not the markers, you wouldn't nest the Views, you would need two separate Views with distinct settings, one of which would include the search form and the map, the second would simply "listen" to the same URL parameter with a Query Filter, but not include the form controls itself.

If you need more details about that, please open another ticket. You are welcome to assign it to me.

#1122706

Hi Nigel, your code had no effect on sort order for me but I will work with it more in case I mis-implemented it. Wouldn't it be more efficient to filter pre-query though and change the query args so WP doesn't have to sort one way before we re-sort another way? And does sorting the posts after the query work for the entire result set or just one page of paginated results? No need for you to spend a lot of time on this at this point. I'll experiment based on your example.

#1122799

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Scott,

yes, it would, except the ordering by distance by Views is already performed post-query.

I instinctively tried using wpv_post_query first, but had to give up on that.

There is the WP_Query that retrieves the post. Then these are looped over and sorted by distance.

And then my code (it worked on my site) comes along and sorts them again by post title, if no distance search has been set yet.