Answers.Body: <h2>Craft 3</h2>
  
  <p>Thanks to the <code>where()</code> function this has become easy in Craft 3.</p>
  
  <pre><code>{% set query = craft.entries.where([
      'or',
      {'field_lightswitchA': 1},
      {'field_lightswitchB': 1},
  ]) %}
  </code></pre>
  
  <h2>Craft 2</h2>
  
  <p>Custom field parameters are being added with and-logic, so you'd indeed have to
  do it from PHP with custom dbCommands.</p>
  
  <p>You could either approach it with converting a criteria model to a dbCommand object using the <code>buildElementsQuery</code> method, or you listen for <code>elements.buildElementsQuery</code> events and modify the dbCommand if the criteria model meets certain conditions.</p>
  
  <pre><code>craft()-&gt;on('elements.buildElementsQuery', function(Event $event) {
      $criteria = $event-&gt;params['criteria'];
      $dbCommand = $event-&gt;params['query'];
  
      if (isset($criteria-&gt;or_lightswitchA) &amp;&amp; isset($criteria-&gt;or_lightswitchB)) {
          $params = array(
              ':lightswitchA_value' =&gt; $criteria-&gt;or_lightswitchA,
              ':lightswitchB_value' =&gt; $criteria-&gt;or_lightswitchB,
          );
  
          $conditions = array('or',
              'content.field_lightswitchA=:lightswitchA_value',
              'content.field_lightswitchB=:lightswitchB_value',
          );
  
          $dbCommand-&gt;andWhere($conditions, $params);
      }
  });
  </code></pre>
  
  <p>Here's how you'd trigger that listener from your criteria model in Twig.</p>
  
  <pre><code>{% set entries = craft.entries({
      or_lightswitchA: 1,
      or_lightswitchB: 1,
  }) %}
  </code></pre>
  
  <p>Here's example code for the <code>buildElementsQuery</code> approach, the problem with this is that
  once you've converted to dbCommands you can't go back to a criteria model (which would be
  necessary to use the result with Craft's <code>paginate</code> tag for example).</p>
  
  <pre><code>public function myCustomCriteria($criteria, $lightswitchA_value, $lightswitchB_value)
  {
      $dbCommand = craft()-&gt;elements-&gt;buildElementsQuery($criteria);
  
      $params = array(
          ':lightswitchA_value' =&gt; $lightswitchA_value,
          ':lightswitchB_value' =&gt; $lightswitchB_value,
      );
  
      $conditions = array('or',
          'content.field_lightswitchA=:lightswitchA_value',
          'content.field_lightswitchB=:lightswitchB_value',
      );
  
      $dbCommand-&gt;andWhere($conditions, $params);
  
      $results = $dbCommand-&gt;queryAll();
  
      return EntryModel::populateModels($results);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-12T11:05:22.340Z
Answers.Text: carlcs, you're a champ. I've yet to test this (will do really soon), but A+ answer – thanks a lot!
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-12T11:07:07.583Z
Answers.Text: I do actually need to paginate the results, btw – so I'll be going with the event approach.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-12T23:18:14.307Z
Answers.Text: I had to test if the two attributes were actually set for the `$criteria` object, but other than that, this worked like a charm. I did a minor edit to your answer to reflect what actually worked for me in the end. Thanks again!
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2016-04-12T10:33:54.593Z
Answers.LastActivityDate: 2018-06-29T11:23:35.617Z
Answers.LastEditDate: 2018-06-29T11:23:35.617Z
Answers.UserDisplayName: carlcs
Body: <p>I have two Lightswitch fields – <code>lightswitchA</code> and <code>lightswitchB</code> – and I need to pull all entries in a certain section, that have <em>one or both</em> of these fields enabled.</p>
  
  <p>Is it possible to execute such a query with a standard ElementCriteriaModel, or do I need to construct a custom query? If I need to construct a custom query, how would that look (in a template, not a plugin)?</p>
  
  <p>I also need to paginate the results, using the <code>{% paginate %}</code> tag.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-04-12T10:31:01.500Z
Comments.Text: I’m pretty sure you can’t do that natively use the field handle as an attribute (I might be wrong) but you could try it with the search parameter?
Comments.UserDisplayName: Josh Angell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-04-13T02:28:34.323Z
Comments.Text: @JoshAngell Thanks – as it happens, carlcs' answer solved this one for me nicely, but I'd love to see a way to do it using search, too, if you ever get the chance to type it out :)
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Tags: <templating><elementcriteriamodel><lightswitch>
Title: How can I query for entries that have one of two Lightswitch fields enabled?
ThreadText: # How can I query for entries that have one of two Lightswitch fields enabled?
  
  (Asked by: username_0 on 2016-04-12)
  
  I have two Lightswitch fields – `lightswitchA` and `lightswitchB` – and I need to pull all entries in a certain section, that have *one or both* of these fields enabled.
  
  Is it possible to execute such a query with a standard ElementCriteriaModel, or do I need to construct a custom query? If I need to construct a custom query, how would that look (in a template, not a plugin)?
  
  I also need to paginate the results, using the `{% paginate %}` tag.
  
  **Tags:** templating, elementcriteriamodel, lightswitch
  
  > **Comments:**
  > **username_1**: I’m pretty sure you can’t do that natively use the field handle as an attribute (I might be wrong) but you could try it with the search parameter?  
  > **username_0**: @username_1 Thanks – as it happens, username_2' answer solved this one for me nicely, but I'd love to see a way to do it using search, too, if you ever get the chance to type it out :)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2016-04-12. Score: 4 | Accepted answer
  
  ## Craft 3
  
  Thanks to the `where()` function this has become easy in Craft 3.
  
  ```
  {% set query = craft.entries.where([
      'or',
      {'field_lightswitchA': 1},
      {'field_lightswitchB': 1},
  ]) %}
  
  ```
  
  ## Craft 2
  
  Custom field parameters are being added with and-logic, so you'd indeed have to
  do it from PHP with custom dbCommands.
  
  You could either approach it with converting a criteria model to a dbCommand object using the `buildElementsQuery` method, or you listen for `elements.buildElementsQuery` events and modify the dbCommand if the criteria model meets certain conditions.
  
  ```
  craft()->on('elements.buildElementsQuery', function(Event $event) {
      $criteria = $event->params['criteria'];
      $dbCommand = $event->params['query'];
  
      if (isset($criteria->or_lightswitchA) && isset($criteria->or_lightswitchB)) {
          $params = array(
              ':lightswitchA_value' => $criteria->or_lightswitchA,
              ':lightswitchB_value' => $criteria->or_lightswitchB,
          );
  
          $conditions = array('or',
              'content.field_lightswitchA=:lightswitchA_value',
              'content.field_lightswitchB=:lightswitchB_value',
          );
  
          $dbCommand->andWhere($conditions, $params);
      }
  });
  
  ```
  
  Here's how you'd trigger that listener from your criteria model in Twig.
  
  ```
  {% set entries = craft.entries({
      or_lightswitchA: 1,
      or_lightswitchB: 1,
  }) %}
  
  ```
  
  Here's example code for the `buildElementsQuery` approach, the problem with this is that
  once you've converted to dbCommands you can't go back to a criteria model (which would be
  necessary to use the result with Craft's `paginate` tag for example).
  
  ```
  public function myCustomCriteria($criteria, $lightswitchA_value, $lightswitchB_value)
  {
      $dbCommand = craft()->elements->buildElementsQuery($criteria);
  
      $params = array(
          ':lightswitchA_value' => $lightswitchA_value,
          ':lightswitchB_value' => $lightswitchB_value,
      );
  
      $conditions = array('or',
          'content.field_lightswitchA=:lightswitchA_value',
          'content.field_lightswitchB=:lightswitchB_value',
      );
  
      $dbCommand->andWhere($conditions, $params);
  
      $results = $dbCommand->queryAll();
  
      return EntryModel::populateModels($results);
  }
  
  ```
  
  > **Comments:**
  > **username_0**: username_2, you're a champ. I've yet to test this (will do really soon), but A+ answer – thanks a lot!  
  > **username_0**: I do actually need to paginate the results, btw – so I'll be going with the event approach.  
  > **username_0**: I had to test if the two attributes were actually set for the `$criteria` object, but other than that, this worked like a charm. I did a minor edit to your answer to reflect what actually worked for me in the end. Thanks again!  
  
  ---
  
  