Answers.Body: <p>No, but this is possible with a few lines of plugin code. You just need to create a <a href="http://buildwithcraft.com/classreference/etc/db/DbCommand" rel="nofollow">DbCommand</a> object from your criteria model and add your <code>WHERE</code> conditions with a <code>OR</code> operator.</p>
  
  <p>Add this function to your <a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow">Template variable</a> class and you can get the entries from your templates with a single query.</p>
  
  <pre><code>public function getEntriesWithVars($vars)
  {
      $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
      $criteria-&gt;section = 'sectionName';
      $criteria-&gt;limit = null;
  
      $query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);
  
      $conditions = array(
          'or',
          array('in', 'field_customField1', $vars),
          array('in', 'field_customField2', $vars)
      );
  
      $query-&gt;andWhere($conditions);
  
      $results = $query-&gt;queryAll();
  
      return EntryModel::populateModels($results);
  }
  </code></pre>
  
  
  
  <pre><code>{% set vars = ['foo', 'bar'] %}
  {% set entries = craft.myPlugin.getEntriesWithVars(vars) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-27T03:14:24.590Z
Answers.Text: I might be missing something obvious, but I'm getting a "Column not found" error when trying to do this for "field_researchTheme". I've also tried just straight "researchTheme" as the field name. That field definitely exists on this entry type.
Answers.UserDisplayName: JamesNZ
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-22T15:54:45.977Z
Answers.LastActivityDate: 2015-10-22T16:03:38.153Z
Answers.LastEditDate: 2015-10-22T16:03:38.153Z
Answers.UserDisplayName: carlcs
Body: <p>I'm currently doing something like this:</p>
  
  <pre><code>{% set vars = "foo, bar" %}
  
  {% set entries1 = craft.entries.section('sectionName').customField1(vars).limit(NULL) %}
  {% for entry in entries1 %}
      {% if entry not in allEntries %}
          {% set allEntries = allEntries | merge([entry]) %}
      {% endif %}
  {% endfor %}
  
  {% set entries2 = craft.entries.section('sectionName').customField2(vars).limit(NULL) %}
  {% for entry in entries2 %}
      {% if entry not in allEntries %}
          {% set allEntries = allEntries | merge([entry]) %}
      {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>But for each custom field I want to check, it's a trip to the database, and I've currently got to check three custom fields. Is there a way to merge these into one trip? I need to know whether the contents of <code>vars</code> are in either custom field. They might be in one, none of both, for example.</p>
  
Tags: <entries><custom-fieldtype><custom-field>
Title: How to fetch entries based on contents of several custom fields?
ThreadText: # How to fetch entries based on contents of several custom fields?
  
  (Asked by: username_0 on 2015-10-22)
  
  I'm currently doing something like this:
  
  ```
  {% set vars = "foo, bar" %}
  
  {% set entries1 = craft.entries.section('sectionName').customField1(vars).limit(NULL) %}
  {% for entry in entries1 %}
      {% if entry not in allEntries %}
          {% set allEntries = allEntries | merge([entry]) %}
      {% endif %}
  {% endfor %}
  
  {% set entries2 = craft.entries.section('sectionName').customField2(vars).limit(NULL) %}
  {% for entry in entries2 %}
      {% if entry not in allEntries %}
          {% set allEntries = allEntries | merge([entry]) %}
      {% endif %}
  {% endfor %}
  
  ```
  
  But for each custom field I want to check, it's a trip to the database, and I've currently got to check three custom fields. Is there a way to merge these into one trip? I need to know whether the contents of `vars` are in either custom field. They might be in one, none of both, for example.
  
  **Tags:** entries, custom-fieldtype, custom-field
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-10-22. Score: 2
  
  No, but this is possible with a few lines of plugin code. You just need to create a [DbCommand](http://buildwithcraft.com/classreference/etc/db/DbCommand) object from your criteria model and add your `WHERE` conditions with a `OR` operator.
  
  Add this function to your [Template variable](http://buildwithcraft.com/docs/plugins/variables) class and you can get the entries from your templates with a single query.
  
  ```
  public function getEntriesWithVars($vars)
  {
      $criteria = craft()->elements->getCriteria(ElementType::Entry);
      $criteria->section = 'sectionName';
      $criteria->limit = null;
  
      $query = craft()->elements->buildElementsQuery($criteria);
  
      $conditions = array(
          'or',
          array('in', 'field_customField1', $vars),
          array('in', 'field_customField2', $vars)
      );
  
      $query->andWhere($conditions);
  
      $results = $query->queryAll();
  
      return EntryModel::populateModels($results);
  }
  
  ```
  
  ```
  {% set vars = ['foo', 'bar'] %}
  {% set entries = craft.myPlugin.getEntriesWithVars(vars) %}
  
  ```
  
  > **Comments:**
  > **username_2**: I might be missing something obvious, but I'm getting a "Column not found" error when trying to do this for "field_researchTheme". I've also tried just straight "researchTheme" as the field name. That field definitely exists on this entry type.  
  
  ---
  
  