[Resolved] Distance from filter based on parent post address field – part II

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.

Author
Posts
#1547171

Hi
With reference to this old post:
https://toolset.com/forums/topic/distance-from-filter-based-on-parent-post-address-field/
Where Nigel was replying the user to wait for a new version. I am using the latest TMaps version and I have the same issue, could you please point me to a tutorial and/or example on how to use a parent post address field to calculate distance while using the result to filter/order children ?
Thanks
Regards
Nicola

#1547505

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Hello, unfortunately it is still not possible to filter a View of child posts by a field in their parent post. That feature has not been added since the other ticket was submitted. However, if this is the only filter in the View, you may be able to work around the issue:
- Create a View of child posts
- Add a Post Relationship Query Filter and choose this parent/child relationship, set by the current post in the loop
- In the Loop of this View, insert the post title or post link shortcode
- Create a View of the parent posts
- Add the distance filter based on the address field and the User's location
- In the loop of this View, insert the View of child posts using a View shortcode
- Place the View of parent posts in a custom page or template to display the filtered list of child posts

This is a very simple workaround that has some limitations:
- It is not possible to filter by other fields in the child post. You can only filter by the parent post information, since the outer View is a View of the parent post type.
- Pagination and sorting may not work as expected, since the child posts will be grouped by parent post.
- AJAX is not supported in these Views.

It is currently possible to display a calculated distance, using an address field from a related post:
https://toolset.com/errata/maps-cannot-evaluate-the-distance-difference-if-they-are-being-compared-through-fields-in-connected-posts/
Unfortunately this does not mean you can use related post addresses in a front-end search filter yet, so the workaround I described might be the best solution.

Let me know if you have questions about this.

#1547953

Hi Christian,
Still missing this feature is bad news because unfortunately it isn't just about finding a workaround.
I can definetly add an address field to my child post and bulk load it from a file, no problem, and of course use this field to calculate distance in views and filters.
But my users are also given the possibility to add their own parents and children with forms. So the following issues arise:
1. when the parent address is updated manually via an edit form, the address field of all children must be updated too (on submit).
2. when a child is manually added or edited its address must be automatically taken from the parent on submit.
I understand some PHP coding is needed here, where I am not strong at, unless you are available to help (the solution here could be useful to many).
Being able to refer to the parent address through the relationship would have avoided all of this.
Please let me know, thanks
Regards
Nicola

#1548635

Hi Christian,
thinking about it, I just realised that my case it's even worst ! I recalled that my parent-children are linked by a many-to-many relationship, so I should place the address in the intermedate relationship, not on my child CPT, but this wouldn't solve the distance calculation issue, I suppose it would't work either, unless you say different. But because I need the distance calculation more than the many-to-many I will place the address on my child CPT anyways and use the many-to-many like a one-to-many, unless you fix the issue ... if ever !
Kind regards
Nicola

#1548873

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Placing the address on the intermediary post type could be better or worse, but again, it depends on the other filters you want to include and the type of sorting you want to implement. Make the intermediary post type visible, then create a View of the intermediary post type. You can add a distance filter based on the address in the intermediary post type and the location of the current User. Now in the results of the View, you can display information from the parent post, the intermediary post, or the child post, just using standard Types and Views shortcodes. That could be very convenient, but if you need to allow your Users to sort or filter by fields in the parent post type or the child post type, those things are not possible without custom code. You would have to automatically copy the custom field values from the parent or child post into the intermediary post, and sort/filter by the fields in the intermediary post.

It's frustrating that filtering by fields in related posts is not a viable option right now, and I hope the information I am able to offer here helps you make a good decision about how best to proceed based on your project requirements and skillset. I'm able to help troubleshoot any of your code that uses Toolset's APIs. I'm able to provide code examples and advice on how to implement that code. I am able to direct you to examples of other similar tickets here in the forum, and direct you to documentation of key concepts. What I cannot do is produce cut-and-paste solutions for you, so if you need that level of assistance then it is time to consult an independent contractor.

#1549901

Hi Christian,
Reflecting even more on the issue, I think that the solution here could be simpler than expected. For in my case all children always have the same distance from the user of the parent (eg. think of stores and products sold in the store) there is no need to calculate distances at child level ! it would be enough to calculate the parent distance from the user and then list in a view all children belonging to parents within the selected distance range. Would this work ? Am I on the right way ?

One related question: when calculating distances does TMaps use the Google API even though no maps are shown ? if yes does this mean that each calculation is charged back to my google account ?
Thanks
Regards
Nicola

#1551575

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

it would be enough to calculate the parent distance from the user and then list in a view all children belonging to parents within the selected distance range. Would this work ? Am I on the right way ?
Okay I see what you mean, and yes I think it could work...again, based on the filters and sorting you want to apply to the end result list.

One related question: when calculating distances does TMaps use the Google API even though no maps are shown ?
No, distance calculation is a non-proprietary mathematical equation based on the geocoded address information. The API hit to geocode an address happens initially, then that information is stored in a cache for use in any future calculations.

#1553679

Thanks Christian,
for testing I have added an address field to the parent (store) and filled it. I then created a view to loop on parents and show the distance of the store from my pc.
Filter: Show posts within 10km radius of the viewing user's location.
Sort: nothing because there is no distance field to sort by ... how to do this ?
Anyways, the view returns no records found when the filter is on and all of them when off. Expected would be the same result in both cases because all stores have distance < 10km from my pc.
Also, I have entered the following code, but no distance is displayed.

STORE: [wpv-post-title] - ADDRESS: [types field="negozio_indirizzo_completo"][/types]
[wpv-geolocation]
[toolset-maps-distance-value origin_source='visitor_location' target_source='postmeta' postmeta='Negozio_indirizzo_completo' decimals='1' unit='km']
[/wpv-geolocation]

Please advise, thanks
Regards
Nicola

#1554113

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Sort: nothing because there is no distance field to sort by ... how to do this ?
Order by the address field, then a "distance" option will appear. Check out the screenshot here: https://toolset.com/documentation/user-guides/maps/display-on-google-maps/filtering-and-ordering-map-markers-by-distance/#ordering-views-by-distance

Anyways, the view returns no records found when the filter is on and all of them when off. Expected would be the same result in both cases because all stores have distance < 10km from my pc.
Also, I have entered the following code, but no distance is displayed.
Can I see how this is configured in wp-admin? Please provide login credentials in the private reply fields here. I will take a closer look and give you some feedback.

#1556327

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

I'm not able to log into wp-admin because of a security restriction. See the screenshot here.

Just noticed that your site is on HTTP. None of the user location distance features will work on HTTP, only on HTTPS. It's a requirement, otherwise the browser will not be able to determine the User's location.

#1556481

Hi Christian,
sorry, I forgot to remove the US block, now it's ok. Afaik https, I believe my site IS on https, see picture.
Regards
Nicola

#1556495

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Okay I'll take another look shortly. The link you provided earlier was HTTP, but if you're on HTTPS the User location features should work.

#1559077

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Okay I updated the Page code in the Fusion Builder because it looks like the shortcode syntax was not quite right. Here is the correct syntax:

{!{wpv-geolocation}!}

View Distanza negozio

{!{/wpv-geolocation}!}

You had some extra square brackets in there that were causing problems. Now the View is displayed correctly without the extra wpv-geolocation shortcodes. However, I think there is a problem with your Google Maps API key. I'm attaching screenshots here. One shows an error in the View's distance filter settings, and the other shows an error in the Maps settings page. I'm pretty sure they are both symptoms of the same problem. Please go to Toolset > Settings > Maps tab, and click "CHECK API" to see the error message:

REQUEST_DENIED - This API project is not authorized to use this API.

This means that the maps plugin cannot make distance calculations or use the Google Maps autocomplete features, and both of those features are critical to Toolset Maps functionality. Please go to the Google Cloud Developers console and update your API Key configurations. At least the following Google API services should be enabled for the API key you are using on the site:
- Geocoding API
- Maps JavaScript API
- Places API

Other API keys may be enabled as well, but at least these 3 are required for Toolset Maps functionality. For security it is best to create two separate API keys - one with no restrictions, and the other with restrictions. The first key (the key with restrictions) will be used on the front-end of the site, so no one can use those credentials anywhere else and charge your Google Maps account. The second key (the key with no restrictions) will be used on the back-end of the site. Since this key is never visible to the public, security will not be a problem.

Let me know once you have your key(s) set up, and we can continue investigating.

#1559791

Hi Christian,
I supposed that there was somenting wrong with brackets ..... (btw is there any doc explaining how brackets work ?)
I have activated the required APIs. Additionally, I also have Maps Embed, Maps static and Street view static APIs activated, are they needed ? can I disable them ? I see that each API has a cost, I have currently no idea about how many calls my site will generate. For now, just distance calculation is critical for my site, I don't need to show maps with markers, maybe I will in the future.
I have also entered in Toolset Settings a second key with no restrictions, that "seems to be configured properly". In the Google Console it shows a warning triangle aside, am I supposed to do anything about it ?
Thanks
Regards
Nicola

#1561817

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

I'm still seeing an error in wp-admin:
This API key is not authorized to use this service or API. Places API error: ApiTargetBlockedMapError

(btw is there any doc explaining how brackets work ?)
Use square brackets unless you are working in a page builder like Fusion Builder. In Fusion Builder, use the {!{ }!} syntax instead of square brackets.
Additionally, I also have Maps Embed, Maps static and Street view static APIs activated, are they needed ? can I disable them ?
They are not needed. You can disable them.

In the Google Console it shows a warning triangle aside, am I supposed to do anything about it ?
No, it is okay.