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?

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

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') %}
  • 2
    I've just faced this problem, and it looks like in Craft 3 it's looking for true (or false / not true) – Adam Hopkinson May 18 '17 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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