Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-27T17:22:23.173Z
Answers.Text: I'm getting this error "Variable "categories" does not exist."
Answers.UserDisplayName: JBrew30
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-27T17:49:47.450Z
Answers.Text: 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"
Answers.UserDisplayName: JBrew30
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-27T21:07:03.010Z
Answers.Text: You need to update the select field to match, I updated the answer with the new code.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-27T17:05:57.597Z
Answers.LastActivityDate: 2021-05-27T21:06:34.743Z
Answers.LastEditDate: 2021-05-27T21:06:34.743Z
Answers.UserDisplayName: Ben Croker
Body: <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>
  
Tags: <craft3><search><plugin-sprig><category-filter>
Title: How to use Sprig to filter categories using a dropdown
ThreadText: # 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() %}
  
  <div s-replace="#board">
  <form>
      <input sprig s-trigger="keyup changed delay:200ms" id="search" name="search" value="{{ search }}" type="text" placeholder="Search" autocomplete="off">
      <select sprig name="category" id="category" value="{{ category }}" class="form-control">
        <option value="">Any</option>
          {% for category in categories %}
            <option value="{{ category }}">{{ category }}</option>
          {% endfor %}
      </select>
    </form>
  
      <div id="board">
          {% for entry in entries %}
          <div class="mt-4 mb-4">
            {% for category in entry.edition.all()%}
              <strong> {{ category }}</strong>
            {% endfor %}
            <div>{{entry.title}}</div>
            {% for category in entry.authors.all() %}
            {{ category.title }},
            {% endfor %}
          </div>
          {% endfor %}
          {% if entries|length == 0 %}
        <div>No entries found </div>
          {% endif %}
          <div>
        </div>
      </div>
    </div>
  
  ```
  
  **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.
  
  ```
      <select sprig name="categoryId" id="category" value="{{ categoryId }}" class="form-control">
        <option value="">Any</option>
          {% for category in categories %}
            <option value="{{ category.id }}">{{ category }}</option>
          {% endfor %}
      </select>
  
  ```
  
  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.  
  
  ---
  
  