Displaying checked ACF items in loop


I have been using CCS code for displaying companies that offer a certain service.

[loop type=profile checkbox=services value=surv orderby=title]
<li>[field title-link] ([field city])</li>

but after converting it to L&L template I am getting all profiles without filtering.

<Loop type=profile acf_checkbox=services value=surv orderby=title>
    <a href="{Field url}"><Field title /></a> - (<Field city />)

What am I missing, please? :slight_smile:

Thanks a lot, Josef

Hey Josef, welcome to the forum! I’m not well-versed in CCS myself, but I think the issue might be with your value=surv parameter. If you’re trying to filter a loop to only show posts that match that field value, it seems that L&L uses a parameter called custom_field for that instead of value.

Let me know if that works for you!

Hey @benjamin –
I have tried custom_field instead of value but it doesn’t work.
Field (checkbox) named “services” has several values, stored in format “acronym : value”, e.g. “'surv : surveying” in ACF. (Acronym is recommended for easier query as value can be pretty long and with spaces between words.)
And I would like to display just companies that checked that they of surveying as a service.

Pseudo code looks like this:
Query companies
Check what services they offer
If “surv” is checked then display the name of company
Go to beginning

Loop is clear.
But the rest is in fog for me :slight_smile:

Thanks for help.
Cheers, J

It seems I was a little tired yesterday and didn’t quite think this through all the way, but having looked at it with fresh eyes today I’ve figured out what your issue is. Two important factors at play here:

  1. ACF checkbox fields save data as an array (I think that’s what it’s called) so if you just added the tag <Field services /> to one of your profile posts, you’d see that the data was saved in a format that looks like ["Checkbox 1 value","Checkbox 3 value","Checkbox 8 value"] if your first, third, and eighth ACF checkboxes were checked on that post.
  2. The acf_checkbox=field_name query parameter is designed to allow you to loop through all the checked items on a single post. It’s not a query parameter that can be used to filter another query, as you’ve done in your markup.

You might want to read this forum post by Eliot to better understand the different ways of choosing what posts you want to display in your query. Since ACF checkbox custom field values are stored as an array and not a simple string, you can’t use custom_field to filter your query, so instead you’ll want to use the field attribute documented here.

This means that you’ll probably want to use attributes like this to filter your query: field=services field_compare=in field_value=surv.

1 Like

Hi @benjamin –

Awesome, now it works as it should.
Thanks a lot for explanation.

I am working on an universal template that could be used with parameter, e.g. [template id=2333 whatservice=surv], for all services.

But it seems that I can’t figure it out :frowning:

How should I pass value of variable to loop, please?

<Get local=whatservice /> <!-- here it really gets parameter from template shortcode

<!-- And here I would like to check against field_value as variable -->
 <!-- I have tried almost everything but I probably don't get the concept behind -->

<Loop type=profile field=services field_compare=in field_value="{Get local}" orderby=title>

    <a href="{Field url}"><Field title /></a> - (<Field city />)

Thanks, J

You’re like 99% of the way there! You’ve figured out that using <Get local=whatservice /> grabs the local variable you’ve defined in your shortcode and you’ve sorta figured out the correct syntax to use tags in attributes when you wrote field_value="{Get local}". Your issue is that Get local doesn’t tell L&L what variable you’re trying to get because you haven’t written the variable name.

So all you need to do is write field_value="{Get local=whatservice}" and then you should be good to go.

1 Like

Yay, it works. Thanks. :heart_eyes:

Is it possible to pass more parameters, e.g. whatservice and whatcounty?
I have tried to store them in local and local_2 but it doesn’t do anything…

It works in Loop, e.g. <Loop type=profile field=services field_compare=in field_value="{Get local=whatservice}" field_2=zipcode field_compare_2=in field_value_2="10221" orderby=title>

I believe you should be able to just use the local attribute to refer to any local variable, no need to append any numbers to it when you’re dealing with multiple variables. There’s no limit to the number of variables you can have on a template since the thing that differentiates them is the variable name, not the attribute used to call the variable. So you could totally have something like <Loop type=profile field=services field_compare=in field_value="{Get local=whatservice}" field_2=zipcode field_compare_2=in field_value_2="{Get local=whatcounty}" orderby=title>

The reason some other attributes and parameters need that little _2 appended to them (like field and field_2 in your example) is that in those particular cases, the order matters because you’re doing a comparison. The logical statement “thing1 is inside thing2” means something different from the logical statement “thing2 is inside thing1”, so the L&L template language often uses that kind of _2 _3 formatting when the order of the attributes matters. It’s also used when there are multiple attributes that need to be associated with the same tag. But for local variables, the order doesn’t matter and you’re only ever associating one attribute with the tag (Get in your case) so the attribute local is the only one that needs to exist.

1 Like