11

This is super simple, and I don't get why it's not working.

{% set results = craft.entries.section('events').lightSwitchField('1') %}

Returns all entries in Events where the light switch field is green.

{% set results = craft.entries.section('events').lightSwitchField('0') %}

Returns all entries in Events. Period. Not all entries that the lightswitch is set to off.

I want to get all entries where the lightswitch field is off. Later I'm going to chain some other fields to the query too.

What have I not understood about how this works?

2
  • Have you tried using true and false in place of '1' and '0'? Oct 14, 2015 at 16:19
  • I had, it makes no difference. Answer was provided by Lindsey below: it's a Craft quirk :/ Oct 15, 2015 at 8:41

1 Answer 1

24

This is a known behavioral issue. PHP considers 0, "0", false, and similar values to be “empty”, and Craft only factors most criteria param values into the query if they are not empty. So setting a Lightswitch field’s criteria param to '0' is the same as not setting it at all.

The workaround is to use not 1:

{% set results = craft.entries.section('events').lightSwitchField('not 1') %}
1
  • 2
    I've just faced this problem, and it looks like in Craft 3 it's looking for true (or false / not true) May 18, 2017 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.