Skip Navigation

[Resolved] Conditional block. Only show the block if the view inside it returns any records

This support ticket is created 3 years, 9 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.

Our next available supporter will start replying to tickets in about 6.72 hours from now. Thank you for your understanding.

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: Africa/Casablanca (GMT+01:00)

This topic contains 1 reply, has 2 voices.

Last updated by Jamal 3 years, 9 months ago.

Assisted by: Jamal.

Author
Posts
#1976103

Hello Support

I have created two custom post types.

Lesson
Downloads

One Lesson can have multiple downloads
One download can be related to multiple lessons.

So I have created a Many to Many relationsship between lesson and downloads.

I am building the lesson content template.

In the lesson content template I want to display a block with a view of the related downloads, but ONLY if there are any related downloads to the lesson.

There is no point ins showing a Title "Lesson downloads" if there are no lessons to display.

So how do you build a conditional Gutenberg block where the condition is, "are there any records returned by the query"?

Cheers

#1976595

Hello and thank you for contacting the Toolset support.

There is currently no way to calculate the count of results of a view outside of the view(in the wrapper conditional block). So, you will have to create a custom function that calculates the count, register it in Toolset settings, then use it inside the conditional block.
- https://toolset.com/documentation/programmer-reference/views/using-custom-functions-in-conditions/

In this custom function you can either use:
- The toolset_get_related_posts function from our relationship api https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
- Or the get_view_query_results from our views api https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
Both functions will return an array of results, you can check if it is empty using the empty php function. Maybe this thread may help. https://toolset.com/forums/topic/conditional-output-with-found-count-or-items-count/#post-559140

I hope this helps. Let me know if you have any questions.