Answers.Body: <p>I would construct an array of the category slugs outside of your childCategories loop (using twig's <a href="http://twig.sensiolabs.org/doc/filters/merge.html" rel="nofollow">merge filter</a>:</p>
  
  <pre><code>{% set queryCatSlugs = [] %}
  {% for category in query %}
    {% set queryCatSlugs = queryCatSlugs|merge([category.slug]) %}
  {% endfor %}
  </code></pre>
  
  <p>Then your childCategories loop can use twig's <a href="http://twig.sensiolabs.org/doc/templates.html#containment-operator0" rel="nofollow">containment operator</a> (<code>in</code>) for the test:</p>
  
  <pre><code>{% for childCategory in childCategories %}
    {% if childCategory.slug in queryCatSlugs %}
      ... matches ...
    {% endif %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-30T08:35:38.747Z
Answers.Text: That worked perfectly! Wasn't aware of the merge filter, will remember that one! Thanks for the help!
Answers.UserDisplayName: Jason Mayo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-29T17:32:45.173Z
Answers.LastActivityDate: 2015-06-29T17:32:45.173Z
Answers.UserDisplayName: Marion Newlevant
Answers.Body: <p>In Craft 3 you don't have to iterate through the <code>chidlCategories</code> manually, you could use the <code>column</code> filter instead, like this: </p>
  
  <pre><code>{% set queryCatSlugs = childCategories.all() | column('slug') %}
  {% if slugToCheck in queryCatSlugs %}
      ....
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-08T10:23:11.157Z
Answers.LastActivityDate: 2019-12-08T10:23:11.157Z
Answers.UserDisplayName: Ottó Radics
Body: <p>I'm trying to check to see if a particular category (In a loop) exists in a query submitted from a search form.</p>
  
  <p>At the moment I'm doing this inside a 'childCategory' loop...</p>
  
  <pre><code>{% for childCategory in childCategories %}
  
      {% for category in query %}{% if childCategory.slug == category.slug %} checked{% endif %}{% endfor %}
  
  {% endfor %}
  </code></pre>
  
  <p>EUGH right?</p>
  
  <p>Is there a better way (Surely!) to check to see if the cateogry exists? I've tried the length and indexOf filters as well as an IN conditional. Nothing works.</p>
  
  <p>The query is being set like this:</p>
  
  <pre><code>{% set query = craft.request.getParam('category') %}
  {% set queryCategories = craft.categories.group('opportunities').slug(query) %}
  </code></pre>
  
Tags: <categories><search><queries>
Title: Check if Category is in Query
ThreadText: # Check if Category is in Query
  
  (Asked by: username_0 on 2015-06-29)
  
  I'm trying to check to see if a particular category (In a loop) exists in a query submitted from a search form.
  
  At the moment I'm doing this inside a 'childCategory' loop...
  
  ```
  {% for childCategory in childCategories %}
  
      {% for category in query %}{% if childCategory.slug == category.slug %} checked{% endif %}{% endfor %}
  
  {% endfor %}
  
  ```
  
  EUGH right?
  
  Is there a better way (Surely!) to check to see if the cateogry exists? I've tried the length and indexOf filters as well as an IN conditional. Nothing works.
  
  The query is being set like this:
  
  ```
  {% set query = craft.request.getParam('category') %}
  {% set queryCategories = craft.categories.group('opportunities').slug(query) %}
  
  ```
  
  **Tags:** categories, search, queries
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-06-29. Score: 3 | Accepted answer
  
  I would construct an array of the category slugs outside of your childCategories loop (using twig's [merge filter](http://twig.sensiolabs.org/doc/filters/merge.html):
  
  ```
  {% set queryCatSlugs = [] %}
  {% for category in query %}
    {% set queryCatSlugs = queryCatSlugs|merge([category.slug]) %}
  {% endfor %}
  
  ```
  
  Then your childCategories loop can use twig's [containment operator](http://twig.sensiolabs.org/doc/templates.html#containment-operator0) (`in`) for the test:
  
  ```
  {% for childCategory in childCategories %}
    {% if childCategory.slug in queryCatSlugs %}
      ... matches ...
    {% endif %}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: That worked perfectly! Wasn't aware of the merge filter, will remember that one! Thanks for the help!  
  
  ---
  
  ### Answer by username_2 on 2019-12-08. Score: 3
  
  In Craft 3 you don't have to iterate through the `chidlCategories` manually, you could use the `column` filter instead, like this:
  
  ```
  {% set queryCatSlugs = childCategories.all() | column('slug') %}
  {% if slugToCheck in queryCatSlugs %}
      ....
  {% endif %}
  
  ```
  
  
  ---
  
  