Skip Navigation

[Resolved] Can I use conditional output with a repeatable field group

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a repeatable field group that contains several fields. I would like to use a conditional to test if any of the RFG has a value in one specific field. If so, I would like to show some text in a header. If not, I would like to hide the header.

Solution: One way to accomplish this in Toolset alone is with a View of the Repeatable Field Group. Add a post relationship query filter, so that the parent post is set by the current page ( or the current post in the loop if you're displaying the main post in a View ). Then add a Query Filter for the custom field (or fields), and check that the field value is not empty. Set the limit to 1 post, since all you need to know is whether or not one exists. In the wpv-items-found section of the loop, add the heading information you want to display. In the wpv-no-items-found section, delete the text message. This View is now effectively a conditional that tests if any related RFG have content in the custom field(s) in your query filter.

If you need something more flexible, a custom shortcode approach is probably more practical. You can use the toolset_get_related_posts API to get all the RFGs for a specific post, then loop over them and inspect their post meta values. I can help with toolset_get_related_posts if you need more information. Basically an RFG is treated as a child of the main post. The slug of the relationship is identical to the RFG slug.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
https://developer.wordpress.org/reference/functions/get_post_meta/

This support ticket is created 5 years, 11 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by matthewL-7 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#1169630

Hey

Is it possible to use conditional output with repeatable fields.

Basically all I want to do is check if any of the repeatable fields for a specific post have any content in them. If they are I will display a heading. If all blank heading won't display.

The reason the heading needs to work like this is it checks another field for content also not just this repeatable field group.

Thanks.

#1169968

One way to accomplish this in Toolset alone is with a View of the Repeatable Field Group. Add a post relationship query filter, so that the parent post is set by the current page ( or the current post in the loop if you're displaying the main post in a View ). Then add a Query Filter for the custom field (or fields), and check that the field value is not empty. Set the limit to 1 post, since all you need to know is whether or not one exists. In the wpv-items-found section of the loop, add the heading information you want to display. In the wpv-no-items-found section, delete the text message. This View is now effectively a conditional that tests if any related RFG have content in the custom field(s) in your query filter.

If you need something more flexible, a custom shortcode approach is probably more practical. You can use the toolset_get_related_posts API to get all the RFGs for a specific post, then loop over them and inspect their post meta values. I can help with toolset_get_related_posts if you need more information. Basically an RFG is treated as a child of the main post. The slug of the relationship is identical to the RFG slug.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
https://developer.wordpress.org/reference/functions/get_post_meta/

#1170508

Hey Christian

Thanks for the detailed reply.

Your first point makes complete sense and is definitely an option albeit a little messy.

Your second point, I will be honest completely lost me. I understand what a custom shortcode is and have used some examples off the Toolset forums in the past but I just have no idea where to start with this.

As a third option I could have a tick box that a user ticks above the repeatable field group to enable the heading basically telling it look I have filled in the below fields so I have ticked this box, then I check if that has been fixed.

From a site speed perspective which do you think is the best option for me to go with?

#1170718

The third option is definitely the most performant and requires the fewest queries, though it requires manual management on the backend.

For a few examples of the second option, you can check these other forum posts about looping over RFG:
https://toolset.com/forums/topic/how-to-display-repeatable-field-groups-with-php/
https://toolset.com/forums/topic/repeatable-group-with-php/
https://toolset.com/forums/topic/loop-through-the-repeatable-field-groups-using-php-code/

And the shortcode API documentation for WordPress:
https://codex.wordpress.org/Shortcode_API

#1170766

Hi Christian

Hmm OK I might just go with option 3.

Ran into another issue though, can I use an AND and an OR statement in a conditional together?

so if (X) AND (X) OR (X) the show content.

Is this possible?

#1170788

Yes, simple combinations are possible. You must use parenthesis and order of operations, like in algebra.

[wpv-conditional if="( ( '1' eq '1' AND '2' eq '3' ) OR '3' eq '3' )"]
something here
[/wpv-conditonal]
#1171198

My issue is resolved now. Thank you!