Skip Navigation

[Resolved] Conditional output in relations field in a view shortcode

This support ticket is created 3 years, 10 months 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 3 replies, has 2 voices.

Last updated by matsF 3 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#1964481
screencapture-labb-gotlandsmuseum-se-2021-02-23-18_50_59.jpg
Capture_toolset_01.jpg
Capture_toolset_02.jpg

Hi,

I've built a museum site with Toolset and Beaver Builder where I'm showing Events (from Events Calendar) and other info in a side column/side bar common for Pages, some of the Pages are for descriptions of locations, eg of The Art Museum.

I've created a new CPT called Platserinfo (Places Information) where my client will create places and put address, opening hours, entrance fee etc. That post type is not shown publicly. It's just like a form with custom fields to put data in and display in different places on the site. Pages and Events both have a relation field connected to this Places Information CPT, so they can "speak to" each other.

So far most things are working fine. With some great help from the Toolset support I managed even to pick data from parent location pages as some pages have the same "parent", ie share the same side column info. I also put conditional output on the fields to hide them if they're empty.

Though, there is one problem. When displaying an events view in the Page side column I managed to list the Events connected to the common CPT post/place, let's say The Art Museum. And if some of the places at the moment doesn't have an upcoming event, there are no events to show, so there are no events visible. And that's fine. But many of the Pages are not meant to be used for locations and therefore my client won't pick any place from the relation field. And on those pages all of the upcoming events are shown since there's nothing to filter them.

This is the shortcode/Content Template for the view/list:

[wpv-view name="events-platser" wpvrelatedto="[wpv-post-id item="@platsinfo.parent"]"]

Would it be a way to solve the problem to put a conditional statement around (or inside) this shortcode to check if "@platsinfo.parent" is empty? I'm not sure about the syntax here, though...

Or is there a way to do this in the filter section in the view? See attached images!

My solution at the moment is a separate Page Template for the non-location Pages. But since the site already is very complex I want to simplyfy for my client. Hope it's possible.

Thanks in advance!

Mats

#1965025

Hi Mats,

Thank you for contacting us and I'd be happy to assist.

If your goal is to only show the view when the post ID of the related "platsinfo.parent" exists, you can wrap its shortcode in a conditional check like this:
( ref: https://toolset.com/documentation/legacy-features/views-plugin/using-shortcodes-in-conditions/ )


[wpv-conditional if="( '[wpv-post-id item='@platsinfo.parent']' ne '' )"]
[wpv-view name="events-platser" wpvrelatedto="[wpv-post-id item="@platsinfo.parent"]"]
[/wpv-conditional]

As a result, the view will be only shown when the value from [wpv-post-id item='@platsinfo.parent'] is not equal to empty (or in other words exists).

regards,
Waqar

#1965321

Many thanks Waqar!

That solution was the one I was looking for. Works great.

Regards

Mats

#1965323

My issue is resolved now. Thank you!