Skip Navigation

[Resolved] Posts with multiple addresses, show closest address/location from View search

This support ticket is created 6 years 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
- 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)

This topic contains 6 replies, has 2 voices.

Last updated by Nigel 6 years ago.

Assisted by: Nigel.

Author
Posts
#1140158

Tell us what you are trying to do? I have a custom post type (therapist listing) that has up to three locations / street addresses as values/fields. Think of this as a directory listing, with multiple service locations. The views search appears to work fine, and will check all three when searching, and return the listing/post if at least one of the three addresses falls within the search radius. The listings that return all show the primary/first street address though, and what I would like to happen is instead the show the address that is closest/matching search options. E.g. if #2 street address for a listing falls within the search radius, when the listing is shown in the results, have that street address appear. If there are more than one street address for the listing that fall within the search radius, show the first one that is found as part of the results (not them all).

Is there any documentation that you are following? Just looking at example Toolset sites

Is there a similar example that we can see? n/a

What is the link to your site? /therapist-search/

#1140332

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Mark

We have an imminent release of Maps 1.6 which covers some of the issues you talk about here, and I would like to do some testing with that version to see if it changes anything.

Let me do that and then I'll get back to you.

#1140366

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Mark

OK, I set up a test with Maps 1.6 to look at what you are describing, and I don't see any way of achieving what you want.

The distance filtering is part of Views and Maps.

Remember, what's going on here is that you set up a View to query for posts where the address falls within the map search radius (and it is able to handle the case where a post has multiple address fields, and only one of them needs to fall within the search radius for the post to qualify).

So the query returns a list of matching posts, and then we iterate over them to output the desired fields.

When it comes to outputting the address, that is done with the types shortcode, which provides a separator attribute to specify how multiple field values should be displayed. But the shortcode is ignorant of the distance calculations that were performed relative to the search location by the View. It is not possible to order the multiple field values accordingly, and it is not something that is readily customisable.

If you have a business listing, and that business has several locations, I think what you would need to do is to make a post type for the location which has an address field and which is a child post of the business. Your location search would then query the location posts directly (rather than the business posts) by distance, and when iterating over the results you could output fields from the parent business alongside the address & distance fields for the child location posts.

#1140370

Hey there Nigel,

Thanks for going through this, and I understand what you are saying. I do have a question though: How does sorting by location/distance work? Surely there is some distance value used there? Although inefficient, do you know of a way to have each of the results when rendering have their distance recalculated against the search filter, and show the shortest distance?

Side question that has just come up from me thinking about this -- one of the addresses ends up showing up on the map, is this the shortest distance one, or the first one found? And secondly, any way to make that pinned location on the map have a hover-over/on-click with some of the details of that result?

Thanks,
Mark

#1140407

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Mark

The distance filters work in two stages. First, all posts of the type being queried have their distance calculated to narrow the universe of possible posts down to the ones within the search radius. This list of posts, which may be sorted by distance, is then passed to the standard query (built on WP_Query) where any other filters are also applied and the ordering maintained.

To display the distance you can insert this via the Fields and Views button which will insert the new toolset-maps-distance-value shortcode, which will calculate the distance of the current post to the specified location (which may be the location entered in the search filters).

In my testing I found that when there are multiple addresses the distance it displayed was the shortest distance, i.e. the lowest of the distances for all values of the address field, but you should double-check that yourself with your own data to be sure.

Side question: where you insert the markers in your View you can include fields from the related post, such as the title and address, in a pop-up as described here: https://toolset.com/documentation/user-guides/display-on-google-maps/displaying-markers-on-google-maps/#marker-title-and-popup

#1140421

Thanks for that Nigel, this is really helpful.

Here is what I am wondering: if it's possible to show the distance value (through that shortcode), I wonder if we can use that shortcode as part of a conditional statement, to show the street address it matches? Thoughts on this? Trying to come up with a solution with all the tools at hand, even if not elegant.

Regards,
Mark

#1141314

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Thinking it through I can't see a solution—inelegant or not—based upon repeating address fields.

I think you need a solution that uses child posts (or address fields in repeatable field groups, which is essentially the same thing), I can't see any alternative.