<p>You need to apply the category filter to the entry query.</p>
<pre><code>{% set categoryId = categoryId ?? '' %}

{% set entryQuery = craft.entries ({
    search: search,
    section: 'articles',
    limit: 2,
    relatedTo: categoryId,
}) %}
</code></pre>
<p>And also update the select field to match.</p>
<pre><code>    &lt;select sprig name=&quot;categoryId&quot; id=&quot;category&quot; value=&quot;{{ categoryId }}&quot; class=&quot;form-control&quot;&gt;
      &lt;option value=&quot;&quot;&gt;Any&lt;/option&gt;
        {% for category in categories %}
          &lt;option value=&quot;{{ category.id }}&quot;&gt;{{ category }}&lt;/option&gt;
        {% endfor %}
    &lt;/select&gt;
</code></pre>
<p>See the Sprig filters video for an explanation of how to output and use categories as filters:<br />
<a href="https://putyourlightson.com/sprig/videos#sprig-filters" rel="nofollow noreferrer">https://putyourlightson.com/sprig/videos#sprig-filters</a></p>

CC BY-SA 4.0
2021-05-27T17:22:23.173Z
I'm getting this error "Variable "categories" does not exist."
JBrew30
CC BY-SA 4.0
2021-05-27T17:49:47.450Z
Thanks for the Speedy reply. I then tried `{% set entryQuery = craft.entries ({     search: search,     section: 'articles',     limit: 2,      relatedTo: category,     }) %} ` and the error went away but now when I select a year from the dropdown it gives me "No entries found"
JBrew30
CC BY-SA 4.0
2021-05-27T21:07:03.010Z
You need to update the select field to match, I updated the answer with the new code.
Ben Croker
CC BY-SA 4.0
2021-05-27T17:05:57.597Z
2021-05-27T21:06:34.743Z
2021-05-27T21:06:34.743Z
Ben Croker
<p>I'm having an issue where I'm adding a dropdown to my search to filter through the first parent attribute in a category (Winter 2020, Spring 2021, and so on). The search field works, but nothing happens if I select from the dropdown (Winter 2020).</p>
<p>What am I missing?</p>
<pre><code>{% set search = search ?? ''%}
{% set category = category ?? ''%}

{% set entryQuery = craft.entries ({
    search: search,
    section: 'articles',
    limit: 2, 
    }) %}


{% set myCategoryQuery = craft.categories ({
    group: 'edition', 
    level: 1,
    }) %}

{% set entries = entryQuery.all() %}
{% set categories = myCategoryQuery.all() %}



&lt;div s-replace=&quot;#board&quot;&gt;
&lt;form&gt;
    &lt;input sprig s-trigger=&quot;keyup changed delay:200ms&quot; id=&quot;search&quot; name=&quot;search&quot; value=&quot;{{ search }}&quot; type=&quot;text&quot; placeholder=&quot;Search&quot; autocomplete=&quot;off&quot;&gt;
    &lt;select sprig name=&quot;category&quot; id=&quot;category&quot; value=&quot;{{ category }}&quot; class=&quot;form-control&quot;&gt;
      &lt;option value=&quot;&quot;&gt;Any&lt;/option&gt;
        {% for category in categories %}
          &lt;option value=&quot;{{ category }}&quot;&gt;{{ category }}&lt;/option&gt;
        {% endfor %}
    &lt;/select&gt;
  &lt;/form&gt;

    &lt;div id=&quot;board&quot;&gt;
        {% for entry in entries %}
        &lt;div class=&quot;mt-4 mb-4&quot;&gt;
          {% for category in entry.edition.all()%}
            &lt;strong&gt; {{ category }}&lt;/strong&gt;
          {% endfor %}
          &lt;div&gt;{{entry.title}}&lt;/div&gt;
          {% for category in entry.authors.all() %}
          {{ category.title }},
          {% endfor %}
        &lt;/div&gt;
        {% endfor %}
        {% if entries|length == 0 %}
      &lt;div&gt;No entries found &lt;/div&gt;
        {% endif %}
        &lt;div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;

</code></pre>

<craft3><search><plugin-sprig><category-filter>
How to use Sprig to filter categories using a dropdown
# How to use Sprig to filter categories using a dropdown

(Asked by: username_0 on 2021-05-27)

I'm having an issue where I'm adding a dropdown to my search to filter through the first parent attribute in a category (Winter 2020, Spring 2021, and so on). The search field works, but nothing happens if I select from the dropdown (Winter 2020).
What am I missing?
{% set search = search ?? ''%}
{% set category = category ?? ''%}

{% set entryQuery = craft.entries ({
    search: search,
    section: 'articles',
    limit: 2, 
    }) %}


{% set myCategoryQuery = craft.categories ({
    group: 'edition', 
    level: 1,
    }) %}

{% set entries = entryQuery.all() %}
{% set categories = myCategoryQuery.all() %}





    
    
      Any
        {% for category in categories %}
          {{ category }}
        {% endfor %}
    
  

    
        {% for entry in entries %}
        
          {% for category in entry.edition.all()%}
             {{ category }}
          {% endfor %}
          {{entry.title}}
          {% for category in entry.authors.all() %}
          {{ category.title }},
          {% endfor %}
        
        {% endfor %}
        {% if entries|length == 0 %}
      No entries found 
        {% endif %}

**Tags:** craft3, search, plugin-sprig, category-filter

---

## 1 answers

### Answer by username_1 on 2021-05-27. Score: 3 | Accepted answer

You need to apply the category filter to the entry query.
{% set categoryId = categoryId ?? '' %}

{% set entryQuery = craft.entries ({
    search: search,
    section: 'articles',
    limit: 2,
    relatedTo: categoryId,
}) %}

And also update the select field to match.
    
      Any
        {% for category in categories %}
          {{ category }}
        {% endfor %}
    

See the Sprig filters video for an explanation of how to output and use categories as filters:
https://putyourlightson.com/sprig/videos#sprig-filters

> **Comments:**
> **username_0**: I'm getting this error "Variable "categories" does not exist."  
> **username_0**: Thanks for the Speedy reply. I then tried `{% set entryQuery = craft.entries ({     search: search,     section: 'articles',     limit: 2,      relatedTo: category,     }) %} ` and the error went away but now when I select a year from the dropdown it gives me "No entries found"  
> **username_1**: You need to update the select field to match, I updated the answer with the new code.  

---

