Condition based on ACF being empty or not

Hi team,

I have a very specific question. I have a section on a page with a grid of custom posts which display in a specific order as selected by the user via an ACF posts field. If the user doesn’t select any posts for this specific page, a default random list of the same post type will be displayed.

I can get the customised list of posts to appear or disappear depending on whether they’ve been populated or not by using the ‘provider output’ = ‘is not empty’ condition.

I need to be able to allow the default grid of posts to appear if the custom one hasn’t been selected and is therefore empty. Looking at this logically, I should be able to use a condition on the ‘fallback’ section and set the string to the ACF field supplying the bespoke list above. If this is empty and has no entries, then display the fallback section.

However, this doesn’t seem to work.

I realise this may fall into unsupported territory, but I can do most of this myself. I just need to know if it’s possible to set a condition based on a test for an ACF field being populated or not.

We’ve had this come up before and I know this method can work with the Repeater field. Using type='json' in your dynamic content like below will display false if the repeater is empty. So using that value you can create a condition {{dc:acf:your_field type='json'}} != false to only display if the field is populated. The posts field might work a bit differently, but if you play around with type='json' with an empty field you’ll get an idea of what to do.

Example
{{dc:acf:post_field key='field' type='json'}}

Thank you @charlie

I’m still not sure I understand as I think I’ve tried what you suggested, but I’m unsure if I’m explaining myself very well.

So the custom field, populated via the posts ACF field displays independently. If that’s empty, it doesn’t display and that’s all good.

The one I’m having trouble with is the fallback section where it displays 4 random posts instead of the four curated posts. However, in order for this to appear, I have to check whether the ACF field above has any output. If it doesn’t, then I display the four random ‘fallback’ items. If on the other hand, it contains any output at all, then I hide the fallback section.

Screenshot 2023-01-31 at 22.59.00

In the condition field of the fallback section, I’m checking to see whether the ACF field, ‘other_packages’ is blank / null. If it is empty, this section should appear using the following in the ‘String’ field:

{{dc:acf:post_field key=‘other_packages’ type=‘json’}}

However, this doesn’t work as expected.

Would it help if I provided a private note with login details?

Thank you

Geoff

Yeah send login details in a secure note and we’ll see what’s going on it could be a number of things. ACF sometimes doesn’t work as expected in terms out output. In general type='json' can help you out when you’re not sure what’s really being outputted.

Thank you again, @charlie - I’ve added a note above.

1 Like

So for the post repeater it’s going to return “null” as a string. It’s a little inconsistent what ACF will return you for arrays, sometimes it’ll be “false” though.
The “provider output is empty” condition only works inside the child of a provider or a consumer which is probably where some confusion was.
I updated your site since it’s a staging with new conditions. Let me know if you have any other questions, have a great weekend.

image

Thank you again @charlie

I couldn’t get this to work consistently when source field was changed back to having no selections again, so I’m seeing if I can approach it from a different angle.

Thank you again though for all your help.

Hey @charlie - I just wanted to update this thread based on what I eventually got working.

I think I was over-complicating things by trying to get each one to work independently, checking for output on the other ACF before outputting the section.

Instead, I created a true/false ACF field above the curated post-type selection. Both sections then display based on whether that field returns ‘true’ (for curated entries) or ‘false’ (for fallback / random posts). The only slight flaw is that it will display nothing if the user has neglected to select posts on the backend, even though they changed the switch to true.

However, it works like a charm. Thank you very much for your help once again.

Happy to hear. I’m going to keep that in mind with using another field to hide that’s a great idea. Thanks, Have a great day!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.