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?

Matt Wilcox
  • 3,199
  • 1
  • 14
  • 28

1 Answers1

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') %}
Brandon Kelly
  • 34,307
  • 2
  • 71
  • 137
Lindsey D
  • 23,974
  • 5
  • 53
  • 110