Skip Navigation

[Resolved] IF Condition for a custom field with multiple checkboxes

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

Problem: I would like to use a conditional to test for specific checkboxes being checked.

Solution: Use the Advanced editor for conditionals and use the following structure to test for checked checkboxes:

( ( '[types field="brand-portfolio" option="0"][/types]' = 'eyewear' ) )

Relevant Documentation:
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/#checking-types-checkboxes-field-for-non-empty-value

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.

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 12 replies, has 2 voices.

Last updated by Christian Cox 3 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1725529

Tell us what you are trying to do? I have a custom post type 'brand-owner' and they could be eyewear or lens or both.. so I have a custom field called 'brand-portfolio' which is a multiple checkbox field, connected to this custom post.. I check either eyewear or lens or both.. works fine.. now on the brand-profile template I have My Eyewear and My Lens as 2 views and I want to display these based on the condition that if the brand owner is for 'eyewear' then show eyewear, if it's lens then show lens and if it's both then show both.. but with the multiple check box it works fine if it's either eyewear or lens.. but if it's both then the IF confidion works only on the eyewear and ignores the lens..

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?
Eyewear : hidden link (I don't want to show the Lens part here)
Lenses : hidden link (I don't want to show the Eyewear part here)
Eyewear & Lens : hidden link (This is working fine as I have disable the IF condition.. If I had the IF condition for Eyewear and the IF condition for Lenses... then only the Eyewear is being shown and the Lens does not appear)

#1725905
Screen Shot 2020-08-03 at 12.15.23 PM.png

Hello, the proper syntax depends on the settings for the checkboxes field in this Field Group. I'm sharing a screenshot of a checkboxes group in my local test site. You can see that the raw value to store for option 1 is jamon and the raw value to store for option 2 is 2. In this case, you can write the following conditionals to test for only 1, only 2, or both checkboxes:

[wpv-conditional if="( '[types field="book-cbs-1" output="raw"][/types]' eq 'jamon' )"]
only 1 is checked
[/wpv-conditional]
[wpv-conditional if="( '[types field="book-cbs-1" output="raw"][/types]' eq '2' )"]
only 2 is checked
[/wpv-conditional]
[wpv-conditional if="( '[types field="book-cbs-1" output="raw"][/types]' eq 'jamon, 2' )"]
both are checked
[/wpv-conditional]

If you're having trouble setting up this conditional, please take a screenshot showing the checkboxes options in the field editor screen and I can provide some more guidance.

#1726539
Screenshot 2020-08-04 14.26.30.png
Screenshot 2020-08-04 14.25.52.png
Screenshot 2020-08-04 14.20.13.png

Hi Christian

Thanks for the update..:-)..

I was not using the code editor but I could figure out what you were trying to say .. however I am using 2 IF conditional blocks on the same template.. I tried a workaround where instead of just
IF current-post->brand-portfolio = static value [eyewear]
I changed it to
IF current-post->brand-portfolio = static value [eyewear] OR IF current-post->brand-portfolio = static value [eyewear,lens]

so that in both cases it would show but it had the same result

I am attaching the screenshots and do let me know if you are clear about my requirement

So basicall my requirement is that some brandowners would have a brand portfolio of eyewear and some would have it of lens and some would have both..

In case it's eyewear than only eyewear view should be displayed
In case it's lens then only lens view should be displayed
In case it's both then both views should be displayed..

Please note that they are 2 difference views and each view has its own IF conditional container block and

Regards,
Alim

#1728427

One of your conditionals appears to be set to test "greater than or equal to" instead of "equal to" - please see the screenshot. Please turn on debug mode for each of the conditional blocks and share the results on the front-end of the site - see the screenshot for the location of debug mode toggle. Test one eyewear-only post, one lens-only post, and one post with both checked, and provide the debug information for each of these posts.

#1732697

Hi Christian

Apologies for the late response..

I did enable the debug mode (didn't have the screenshot in your previous mail.. I think it was missed out).. but I figured it out and enabled it.. but what I am not able to figure out is how to provide you the debug info from the front end..;-)..

I assumed that if I go to the front end I would see some thing.. but there's no indication that the debug mode is on or that there's a way to capture the debug info... please advise..

In the meantime, I have a feeling that the result I am getting is logical.. but you know best so please let me know if what I am thinking is right..

If it's a multiple checkbox field the result would be a collection and not a single variable.. so it would be something like [Eyewear,Lens] and not just "Eyewear" or "Lens"... right?

So in this case my condition which is the value of the custom fied 'is equal to' the result would probably not match unless I select only a single checkbox.. so if I were to select Eyewear and the result would be [Eyewear] then maybe the condition would be matched.. but if I were to select 2 checkboxes then my condition of Eyewear == [Eyewear,Lens] would never match.. right?

Basically what I am trying to get would probably be achieved by 'CONTAINS' if there's this option.. or by looping through the collection and checking if my variable matched one or multiple elements in that array.. right?

Not sure if I am making sense but I just thought I'd share my thoughts with you..

Do let me know how to share the debug info..

Have a nice day..:-)..

Regards,
Alim

#1733533

If you didn't see anything output on the front-end after turning on debug mode for the conditional, that indicates some other problem like a mismatched quotation mark, a missing bracket, or other syntax error in the conditional shortcode. It might be easiest if I log in and take a look. Please provide login credentials here and I will take a look and give you some feedback.

#1734919

Maybe I wasn't clear about where to look. I can see debug information here:
https://fourplusmedia.com/youandeyemag/brand-owner/safilo-spa/

Can you tell me which Brand Owner post has both Eyewear and Lens selected? I would like to test that scenario as well.

#1735295

Hi Christian

Thanks for the update..:-)..

Sorry for not enabling debug mode.. but I assumed it's when you create the IF conditions.. (screenshot attached) but I realised that it doesn't really do much.. please do share screenshot and let me know where the debug options are.. it might help in the future..

Here are the details requested..

Link to Brand Owner - FourPlus Vision which should show Eyewear Brands as well as Lens Brands

hidden link

Link to Edit Custom Post FourPlus Vision (to confirm that it does have Eyewear and Lens)

hidden link

Link to Edit Content Template (to check if the IF conditions are in place)

hidden link

Do let me know if you need anything else.

Have a nice day..:-)..

Regards,
Alim

#1736203
Screen Shot 2020-08-12 at 11.45.18 AM.png
Screen Shot 2020-08-12 at 11.37.50 AM.png

Okay I have updated the conditionals to use the same syntax in the Advanced Editor I mentioned in my previous comment https://toolset.com/forums/topic/if-condition-for-a-custom-field-with-multiple-checkboxes/#post-1725905 :
Eyewear or Both:

  ( ('[types field="brand-portfolio" output="raw"][/types]' = 'eyewear' ) OR ( '[types field="brand-portfolio" output="raw"][/types]' = 'eyewear, lens' ) ) 

Lens or Both:

  ( ('[types field="brand-portfolio" output="raw"][/types]' = 'lens' ) OR ( '[types field="brand-portfolio" output="raw"][/types]' = 'eyewear, lens' ) ) 

Now in the FourPlus Vision post, I can see debug information as shown in the screenshots here. Is it possible that a Brand Owner will have "Contact Lens" or "Instruments & Equipment" checked as well?

#1736657

Hi Christian

Thanks for the update..:-)..

It's strange about the enable debug method.. because it's exactly what I had done earlier and I am not sure why it didn't work.. Anyways I am glad it's working..:-)..

I realised that you manually had to do the code by using Advanced Editor.. I would have never figured that out..;-).. so it's a good learning for me..

And yes, going ahead, I would add Brand Owners who could have "Contact Lens" and "Instrument & Equipment" checked as well... Could we have something updated to ensure they are also taken care of?

Thanks in advance.

Have a nice day..:-)..

Regards,
Alim

#1737693

Okay great, I have updated the conditionals a bit. I was reminded of a better way to test checkboxes in this documentation:
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/#checking-types-checkboxes-field-for-non-empty-value

Now in the Advanced Condition editor for Eyewear:

( ( '[types field="brand-portfolio" option="0"][/types]' = 'eyewear' ) )

...or for Lens...

( ( '[types field="brand-portfolio" option="1"][/types]' = 'lens'' ) )

This approach is more flexible and allows for any number of other options in the checkboxes group. I believe it's working now, so I have turned off debugging for the two conditional blocks. Feel free to test and let me know if you see problems.

#1740033

Thanks Christian.. the code seems much nicer and easier to read as well.. and I am glad it could be resolved... I assumed we'd need a workaround but I am glad this feature is something that's available.

I am marking this issue as resolved but would like to check with you on an unrelated topic.. the status of the next release of view.. it's for this ticket that's already closed... https://toolset.com/forums/topic/post-relationship-filters-in-an-archive/

Just wanted to know if there's some sort of an approximate date by when I'd be able to use the filters option as there's quite a few of them that rely on relationships between posts...

Thanks again for the prompt and efficient support.

Have a nice day..:-)..

Regards,
Alim

#1741049

I haven't yet received a release date scheduled for the final release of these updates, so I can only give you an estimate. My best guess is another week or so to gather beta issues and then another 2 weeks to resolve those issues. So I am guessing around 3 weeks from now but that's just speculative. As soon as I have more definite information about the release date I'll let you know.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.