Answers.Body: <p>Crisis averted, thanks largely in part to the versitile nature of the relatedTo targetElement processing. The relevation came that I could feed an array of category ids (inclusive) into that element, and use a single relatedTo "and" parameter overall to get my intended filtering:</p>
  
  <pre><code>{% set params = {
      section: 'technologies',
      limit: null
  } %}
  
  {% set paramRelation = ['and'] %}
  {% if technologyIds|length %}
      {% set paramRelation = paramRelation|merge([{ targetElement:technologyIds }]) %}
  {% endif %}
  {% if institutionId != "" %}
      {% set paramRelation = paramRelation|merge([{ targetElement:institutionId }]) %}
  {% endif %}
  {% if paramRelation|length &gt; 1 %}
      {% set params = params|merge({relatedTo: paramRelation}) %}
  {% endif %}
  
  {% set technologyResults = craft.entries(params) %}
  
  {% paginate technologyResults.limit(10) as technologies %}
      ...
  {% endpaginate %}
  </code></pre>
  
  <p>It's actually more simple this way as well, but I had initially underestimated how much heavy lifting the targetElement piece could do. Once again, Craft exceeds expectations. Thanks!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-22T16:28:46.403Z
Answers.LastActivityDate: 2015-05-22T16:28:46.403Z
Answers.UserDisplayName: Marcus
Body: <p>I'm attempting to build a filter that queries entries of a section based on two criteria: A selection of one or more categories (inclusive), and a single related entry (exclusive).</p>
  
  <p>Specifically, the entries are from the "technologies" section. Each technology is related to one or more categories and a single "institution" entry.</p>
  
  <p>The first step is to allow a user to, for example, select two categories (like "Information Technology" and "Medical Devices") and have the results be inclusive. Easily done with a relatedTo "or" parameter.</p>
  
  <p>But next, I'd like the user to be able to select a single institution, and return only the previous results that are also associated with that institution (like "UW-Oshkosh"). This filter requires an additional relatedTo "and" parameter.</p>
  
  <p>My attempt to add two different relatedTo parameters to the same query (detailed below) doesn't fly -- it will only honor the last relatedTo request:</p>
  
  <pre><code>{% set params = {
      section: 'technologies',
      limit: null
  } %}
  
  {% if technologyIds|length %}
      {% set paramTechnologyRelation = ['or'] %}
      {% for technologyId in technologyIds %}
          {% set paramTechnologyRelation = paramTechnologyRelation|merge([{ targetElement:technologyId }]) %}
      {% endfor %}
      {% set params = params|merge({relatedTo: paramTechnologyRelation}) %}
  {% endif %}
  
  {% if institutionId != "" %}
      {% set paramInstitutionRelation = ['and'] %}
      {% set paramInstitutionRelation = paramInstitutionRelation|merge([{ targetElement:institutionId }]) %}
      {% set params = params|merge({relatedTo: paramInstitutionRelation}) %}
  {% endif %}
  
  {% set technologyResults = craft.entries(params) %}
  
  {% paginate technologyResults.limit(10) as technologies %}
      ...
  {% endpaginate %}
  </code></pre>
  
  <p>I'm trying to think of other approaches -- Is is possible to run a second query on an array of entries? Any ideas on how to best approach this? Thanks for any insights.</p>
  
Tags: <templating><relations><queries><logic>
Title: RelatedTo Query Using Both "and" and "or"
ThreadText: # RelatedTo Query Using Both "and" and "or"
  
  (Asked by: username_0 on 2015-05-22)
  
  I'm attempting to build a filter that queries entries of a section based on two criteria: A selection of one or more categories (inclusive), and a single related entry (exclusive).
  
  Specifically, the entries are from the "technologies" section. Each technology is related to one or more categories and a single "institution" entry.
  
  The first step is to allow a user to, for example, select two categories (like "Information Technology" and "Medical Devices") and have the results be inclusive. Easily done with a relatedTo "or" parameter.
  
  But next, I'd like the user to be able to select a single institution, and return only the previous results that are also associated with that institution (like "UW-Oshkosh"). This filter requires an additional relatedTo "and" parameter.
  
  My attempt to add two different relatedTo parameters to the same query (detailed below) doesn't fly -- it will only honor the last relatedTo request:
  
  ```
  {% set params = {
      section: 'technologies',
      limit: null
  } %}
  
  {% if technologyIds|length %}
      {% set paramTechnologyRelation = ['or'] %}
      {% for technologyId in technologyIds %}
          {% set paramTechnologyRelation = paramTechnologyRelation|merge([{ targetElement:technologyId }]) %}
      {% endfor %}
      {% set params = params|merge({relatedTo: paramTechnologyRelation}) %}
  {% endif %}
  
  {% if institutionId != "" %}
      {% set paramInstitutionRelation = ['and'] %}
      {% set paramInstitutionRelation = paramInstitutionRelation|merge([{ targetElement:institutionId }]) %}
      {% set params = params|merge({relatedTo: paramInstitutionRelation}) %}
  {% endif %}
  
  {% set technologyResults = craft.entries(params) %}
  
  {% paginate technologyResults.limit(10) as technologies %}
      ...
  {% endpaginate %}
  
  ```
  
  I'm trying to think of other approaches -- Is is possible to run a second query on an array of entries? Any ideas on how to best approach this? Thanks for any insights.
  
  **Tags:** templating, relations, queries, logic
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-05-22. Score: 6 | Accepted answer
  
  Crisis averted, thanks largely in part to the versitile nature of the relatedTo targetElement processing. The relevation came that I could feed an array of category ids (inclusive) into that element, and use a single relatedTo "and" parameter overall to get my intended filtering:
  
  ```
  {% set params = {
      section: 'technologies',
      limit: null
  } %}
  
  {% set paramRelation = ['and'] %}
  {% if technologyIds|length %}
      {% set paramRelation = paramRelation|merge([{ targetElement:technologyIds }]) %}
  {% endif %}
  {% if institutionId != "" %}
      {% set paramRelation = paramRelation|merge([{ targetElement:institutionId }]) %}
  {% endif %}
  {% if paramRelation|length > 1 %}
      {% set params = params|merge({relatedTo: paramRelation}) %}
  {% endif %}
  
  {% set technologyResults = craft.entries(params) %}
  
  {% paginate technologyResults.limit(10) as technologies %}
      ...
  {% endpaginate %}
  
  ```
  
  It's actually more simple this way as well, but I had initially underestimated how much heavy lifting the targetElement piece could do. Once again, Craft exceeds expectations. Thanks!
  
  
  ---
  
  