Skip Navigation

[Resolved] Map based on taxonomy field of a related post

This support ticket is created 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 13 replies, has 3 voices.

Last updated by Arno 3 years ago.

Assisted by: Waqar.

Author
Posts
#2277191

Hi,

I have two custom post types with a 1 to 1 relationship. Type A has a taxonomy which has a custom term field to store an address. On the custom post type template of B I'd like to display the addresses of the related post A on a map.

On the custom post type template of B I have a view block that selects the parent post only. The map marker has the view block as its source. But now I'm stuck: how do I select the taxonomy of post A, and it's field?

Alternatively, I tried not to use a view block. In the template of B I can set the map marker source to dynamic/child post/post taxonomies/the taxonomy field. This results in one location for one particular post being displayed, but the rest isn't.

#2277317

Hello,

It is possible with Toolset plugins, you need to setup a taxonomy view with classic editor, for example:
1) Dashboard-> Toolset-> Settings-> General, in section "Editing experience", enable option "Show both the legacy and Blocks interface and let me choose which to use for each item I build"
2) Dashboard-> Toolset-> Views, Create a taxonomy view:
- Query terms of your custom taxonomy
- Filter by:
Taxonomy is set by the current post
- In view's loop, display the map marker shortcode [wpv-map-marker], and setup the address value:
https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#wpv-map-marker
marker_termmeta. Optional. Source of the marker location when displaying a marker from an address termmeta field.
- Outside view's loop, display the map shortcode [wpv-map-render]
https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#wpv-map-render
3) Create a content template, display above taxonomy view shortcode:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-view
4) In the content template of single "template of B" post, display above content template with "item" attribute, for example:
[wpv-post-body view_template="my-ct-slug" item="@relationship-slug.parent"]

More help:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-body
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/item-attribute/

#2277361

Hi Luo,

I appreciate your extensive explanation. This is a new website though, working with the classic editor is not desired. I thought this was a standard use case but I guess I have to rethink and find another option.

Why did my alternative not work? Is that because a map always needs a view?

The goal here is to prevent having a map on too many pages as that slows the site down: https://toolset.com/forums/topic/loading-maps-on-demand/. Hence the separate page where people click to only if they are interested in the map. Can you think of any other options without using legacy stuff?

Thank you,
Arno

#2278189

I think there is a misunderstanding:
1) View block does not support taxonomy view, so you need to use classic editor to setup the taxonomy view I mentioned above
2) Using view created with classic editor won't slow down your website, since it is only shortcodes, it should be more quick.
3) For the issue of your another thread:
https://toolset.com/forums/topic/loading-maps-on-demand/
You can display the map for logged-in users with Toolset Access plugin shortcode, for example:

[toolset_access role="Guest" operator="deny"]
Here display the map shortcodes
[/toolset_access]
#2278505

Ok, I understand that I need to enable legacy in order to have a taxonomy view. It seems quite complex (for a Toolset beginner). I hope there is an easier way.

Here's an example of what I need: a cruise company offers several cruises (custom post type). I was thinking that a taxonomy "Locations" would be handy, where each taxonomy term has an address field. That way I can define each location only once and pick the ones that are relevant for each cruise:

Cruise A: goes to location 1,2,3
Cruise B: goes to location 2,3 4
Cruise C: goes to location 5,7

A custom address field on the cruise post type would require me to define location 2 and 3 more than once. Hence the taxonomy.

Would it make sense to use a custom post type for Locations rather than a taxonomy. Would that make things easier?

#2279627

You are asking for many-to-many relationship between "cruises" and "Locations", both of the methods you mentioned above should be able to work.

If you prefer to setup "Locations" as a custom post type, you can setup many-to-many relationship between "cruises" and "Locations"
https://toolset.com/course-lesson/how-to-set-up-post-relationships-in-wordpress/

And in each single "Cruise" post, display the related "Locations" post with a view block:
https://toolset.com/course-lesson/displaying-related-posts/#displaying-many-related-items

Same as above, in single "Locations" post, you can display related "Cruise" post with a view block.

All above can be setup with blocks editor.

#2279689

Yes, that makes sense. Users need to be able to filter on locations, e.g. to display all cruises that include a particular location. Is it possible to have a view displaying one post type (cruises) and a dropdown listing a related post type (locations)?

I did not find examples for this, so that's why I originally tried to use a taxonomy for locations.

Thank you!

#2280307

Hi,

Thanks for writing back.

Luo is on vacation this week, so I'll be following up on this ticket.

Yes, it is possible to have a view displaying one post type (cruises), that offers a dropdown search field to filter by the related post type (locations).

The relationship search filter can be added the same way as taxonomy and custom fields filters, as explained here:
https://toolset.com/lesson-placement/lesson-placements-1622935-1621213/#how-to-search-by-post-relationships

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2281069

Hi Waqar,

Thank you, I tried that and it works fine. I do have a problem with adding a map though. The Locations post type has a single address field and a repeating field group also with address fields. I want the single address field to show on the map, for all locations associated with cruises that are currently in the view. But it only shows a default map with no markers. The map has the view as a source.

I tried to change the view to pull data from the intermediary post type instead of cruises, but that did not make a difference.

How do I show the addresses?

#2281617

Thanks for writing back and glad that the relationship search filter is working.

To troubleshoot why the location markers are not showing on the map, I'll need to see exactly how this view is set up in the admin area.

Can you please share temporary admin login details, along with the link to the page where this view can be seen?

Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.

#2283457

The admin access details worked, thank you.

I'm currently reviewing the setup and will share the findings, shortly.

Thank you for your patience.

#2286485

Are you still working on this Waqar?

Thanks,
Arno

#2286501

Thank you for waiting and I apologize for the delay in getting back on this.

On the "Activities" page, I noticed that you have a view "Activity grid", that shows the list of "Activity Locations Intermediary Posts".

To show the map markers from the related "location" post's "main-location" field, you'll need to use the map marker shortcode:
https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#wpv-map-marker

To get the 'lat' and 'lon' attributes for that field, you can use the Types field API shortcode:
https://toolset.com/documentation/customizing-sites-using-php/functions/#address

To get the latitude value:


[types field='main-location' item='@activity-location.child' format='FIELD_LATITUDE'][/types]

To get the longitude value:


[types field='main-location' item='@activity-location.child' format='FIELD_LONGITUDE'][/types]

You'll see that the combined shortcode for the map marker is added in a "Fields and Text" block in the view's loop:


[wpv-map-marker map_id='map-20' marker_id='marker-[wpv-post-id]' lat='[types field='main-location' item='@activity-location.child' format='FIELD_LATITUDE'][/types]' lon='[types field='main-location' item='@activity-location.child' format='FIELD_LONGITUDE'][/types]'][/wpv-map-marker]

And as a result, the map markers are now showing from those related "location" posts, on this "Activities" page.

Important note: This approach will work if each 'activity' has only one related "location" post. In case there are multiple "location" posts related to an activity, the address from only one of the locations will show on the map.

To show the map markers from multiple related location posts, you'll need to create a legacy/classic view, as Luo suggested earlier.

I hope this helps and please let me know if you need any further assistance around this.

#2287015

Thank you, that works fine. I do have more questions about the legacy views. Will post those in a new thread.