<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>

CC BY-SA 3.0
2015-06-30T08:35:38.747Z
That worked perfectly! Wasn't aware of the merge filter, will remember that one! Thanks for the help!
Jason Mayo
CC BY-SA 3.0
2015-06-29T17:32:45.173Z
2015-06-29T17:32:45.173Z
Marion Newlevant
<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>

CC BY-SA 4.0
2019-12-08T10:23:11.157Z
2019-12-08T10:23:11.157Z
Ottó Radics
<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>

<categories><search><queries>
Check if Category is in Query
# 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:
{% set queryCatSlugs = [] %}
{% for category in query %}
  {% set queryCatSlugs = queryCatSlugs|merge([category.slug]) %}
{% endfor %}

Then your childCategories loop can use twig's containment operator (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 %}


---

