Answers.Body: <p>Turns out the error was elsewhere in the template where I had forgotten to add the .all() statement. The code in the example is error free.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-22T07:27:49.373Z
Answers.Text: Can you mark this as the correct answer so that this question appears resolved?
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-06-15T10:39:45.487Z
Answers.LastActivityDate: 2021-06-15T10:39:45.487Z
Answers.UserDisplayName: erwinheiser
Answers.Body: <p>One thing that stands out is that you're checking <code>myQuery | length</code> instead of <code>items | length</code>. Though I've tested it and this doesn't actually cause a deprecation error in my setup.</p>
  <p>Have you tried clearing the deprecation warnings in the backend manually? They won't disappear automatically if you change the template, but will stay visible until cleared manually. Try clearing them and reload the template, see if the warning appear again.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-06-15T07:19:32.513Z
Answers.LastActivityDate: 2021-06-15T07:19:32.513Z
Answers.UserDisplayName: MoritzLost
Body: <p>I'm using sprig, following the <a href="https://craftquest.io/courses/commerce-product-filtering-with-sprig" rel="nofollow noreferrer">https://craftquest.io/courses/commerce-product-filtering-with-sprig</a> example. I have 3 selects (thickness, look, finish), each using categories from a different group. _entryId is a passed-in parameter that holds the id of the parent entry in a structure.</p>
  <p>My code is working fine but I keep getting <em>Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them.</em> errors in the cp. My template code is below, what am I missing here?</p>
  <pre><code>{% set thickness = thickness ?? '' %}
  {% set look = look ?? '' %}
  {% set finish = finish ?? '' %}
  
  {% if reset is defined %}
      {% set thickness = '' %}
      {% set look = '' %}
      {% set finish = '' %}
  {% endif %}
  
  {% set thicknessOptions = craft.categories().group('thickness').all() %}
  {% set lookOptions = craft.categories().group('look').all() %}
  {% set finishOptions = craft.categories().group('finish').all() %}
  
  {% set myQuery = craft.entries.section('collections').with([['projectImage', {withTransforms: ['collectionSubcatThumbnail']}]]).descendantOf(_entryId) %}
  
  {% if thickness %}
      {% do myQuery.andRelatedTo(thickness) %}
  {% endif %}
  
  {% if look %}
      {% do myQuery.andRelatedTo(look) %}
  {% endif %}
  
  {% if finish %}
      {% do myQuery.andRelatedTo(finish) %}
  {% endif %}
  
  {% set items = myQuery.all() %}
  
  &lt;!-- block 4 col img --&gt;
  &lt;section class=&quot;blck&quot;&gt;
      &lt;div class=&quot;container&quot;&gt;
          &lt;div class=&quot;mb-5 row align-items-center&quot;&gt;
              &lt;div class=&quot;col-6 col-sm-4 col-md-3&quot;&gt;
                  &lt;p&gt;
                      &lt;button class=&quot;btn btn-light btn-sm&quot; sprig s-val:reset=&quot;1&quot; title=&quot;reset&quot; aria-label=&quot;reset filters&quot;&gt;
                          &lt;small&gt;reset filters&lt;/small&gt;
                          &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;24&quot; height=&quot;24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; class=&quot;feather feather-refresh-cw&quot;&gt;
                              &lt;path d=&quot;M23 4v6h-6M1 20v-6h6&quot;/&gt;
                              &lt;path d=&quot;M3.51 9a9 9 0 0114.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0020.49 15&quot;/&gt;
                          &lt;/svg&gt;
                      &lt;/button&gt;
                  &lt;/p&gt;
              &lt;/div&gt;
              &lt;div class=&quot;col-6 col-sm-4 col-md-3&quot;&gt;
                  &lt;select s-replace=&quot;#query_result&quot; sprig name=&quot;thickness&quot; value=&quot;{{ thickness }}&quot; class=&quot;div-select w-100&quot; aria-label=&quot;Thickness&quot;&gt;
                      &lt;option value=&quot;&quot; selected&gt;Thickness&lt;/option&gt;
                      {% for cat in thicknessOptions %}
                          &lt;option value=&quot;{{ cat.id }}&quot; {{ thickness == cat.id ? 'selected '}}&gt;{{ cat.title }}&lt;/option&gt;
                      {% endfor %}
                  &lt;/select&gt;
              &lt;/div&gt;
              &lt;div class=&quot;col-6 col-sm-4 col-md-3&quot;&gt;
                  &lt;select s-replace=&quot;#query_result&quot; sprig name=&quot;look&quot; value=&quot;{{ look }}&quot; class=&quot;form-select w-100&quot; aria-label=&quot;Look&quot;&gt;
                      &lt;option value=&quot;&quot; selected&gt;Look&lt;/option&gt;
                      {% for cat in lookOptions %}
                          &lt;option value=&quot;{{ cat.id }}&quot; {{ look == cat.id ? 'selected '}}&gt;{{ cat.title }}&lt;/option&gt;
  
                      {% endfor %}
                  &lt;/select&gt;
              &lt;/div&gt;
              &lt;div class=&quot;col-6 col-sm-4 col-md-3&quot;&gt;
                  &lt;select s-replace=&quot;#query_result&quot; sprig name=&quot;finish&quot; value=&quot;{{ finish }}&quot; class=&quot;form-select w-100&quot; aria-label=&quot;Finish&quot;&gt;
                      &lt;option value=&quot;&quot; selected&gt;Finish&lt;/option&gt;
                      {% for cat in finishOptions %}
                          &lt;option value=&quot;{{ cat.id }}&quot; {{ finish == cat.id ? 'selected '}}&gt;{{ cat.title }}&lt;/option&gt;
  
                      {% endfor %}
                  &lt;/select&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/section&gt;
  &lt;section class=&quot;blck&quot;&gt;
      &lt;div class=&quot;container&quot;&gt;
          &lt;div class=&quot;row justify-content-center&quot; id=&quot;query_result&quot;&gt;
              {% if myQuery |length %}
                  {% for item in items %}
                      &lt;div class=&quot;col-6 col-sm-4 col-md-3&quot;&gt;
                          &lt;div class=&quot;mb-5 img-item&quot;&gt;
                              &lt;a href=&quot;{{ item.url }}&quot; class=&quot;img-ratio img-1-1&quot;&gt;
                                  &lt;div class=&quot;in&quot;&gt;
                                      {% set image = item.projectImage[0] ?? null %}
                                      &lt;img class=&quot;media&quot; data-object-fit=&quot;cover&quot; src=&quot;{{ image.url('collectionSubcatThumbnail')}}&quot;&gt;
                                  &lt;/div&gt;
                              &lt;/a&gt;
                              &lt;h6&gt;{{ item.title }}&lt;/h6&gt;
                              &lt;a href=&quot;{{ item.url }}&quot; aria-label=&quot;Discover {{ item.title }}&quot; class=&quot;lnk-underline&quot;&gt;Discover this stone&lt;/a&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  {% endfor %}
              {% else %}
                  &lt;div class=&quot;alert alert-warning&quot;&gt;
                      &lt;p class=&quot;mb-0&quot;&gt;No products with that category or combination of categories. Try resetting the filters or deselecting 1 or more options.&lt;/p&gt;
                  &lt;/div&gt;
              {% endif %}
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/section&gt;
  </code></pre>
  
Tags: <plugin-sprig><category-filter>
Title: Using sprig for category filter but keep getting deprecation errors
ThreadText: # Using sprig for category filter but keep getting deprecation errors
  
  (Asked by: username_0 on 2021-06-14)
  
  I'm using sprig, following the <https://craftquest.io/courses/commerce-product-filtering-with-sprig> example. I have 3 selects (thickness, look, finish), each using categories from a different group. \_entryId is a passed-in parameter that holds the id of the parent entry in a structure.
  
  My code is working fine but I keep getting *Looping through element queries directly has been deprecated. Use the all() function to fetch the query results before looping over them.* errors in the cp. My template code is below, what am I missing here?
  
  ```
  {% set thickness = thickness ?? '' %}
  {% set look = look ?? '' %}
  {% set finish = finish ?? '' %}
  
  {% if reset is defined %}
      {% set thickness = '' %}
      {% set look = '' %}
      {% set finish = '' %}
  {% endif %}
  
  {% set thicknessOptions = craft.categories().group('thickness').all() %}
  {% set lookOptions = craft.categories().group('look').all() %}
  {% set finishOptions = craft.categories().group('finish').all() %}
  
  {% set myQuery = craft.entries.section('collections').with([['projectImage', {withTransforms: ['collectionSubcatThumbnail']}]]).descendantOf(_entryId) %}
  
  {% if thickness %}
      {% do myQuery.andRelatedTo(thickness) %}
  {% endif %}
  
  {% if look %}
      {% do myQuery.andRelatedTo(look) %}
  {% endif %}
  
  {% if finish %}
      {% do myQuery.andRelatedTo(finish) %}
  {% endif %}
  
  {% set items = myQuery.all() %}
  
  <!-- block 4 col img -->
  <section class="blck">
      <div class="container">
          <div class="mb-5 row align-items-center">
              <div class="col-6 col-sm-4 col-md-3">
                  <p>
                      <button class="btn btn-light btn-sm" sprig s-val:reset="1" title="reset" aria-label="reset filters">
                          <small>reset filters</small>
                          <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-refresh-cw">
                              <path d="M23 4v6h-6M1 20v-6h6"/>
                              <path d="M3.51 9a9 9 0 0114.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0020.49 15"/>
                          </svg>
                      </button>
                  </p>
              </div>
              <div class="col-6 col-sm-4 col-md-3">
                  <select s-replace="#query_result" sprig name="thickness" value="{{ thickness }}" class="div-select w-100" aria-label="Thickness">
                      <option value="" selected>Thickness</option>
                      {% for cat in thicknessOptions %}
                          <option value="{{ cat.id }}" {{ thickness == cat.id ? 'selected '}}>{{ cat.title }}</option>
                      {% endfor %}
                  </select>
              </div>
              <div class="col-6 col-sm-4 col-md-3">
                  <select s-replace="#query_result" sprig name="look" value="{{ look }}" class="form-select w-100" aria-label="Look">
                      <option value="" selected>Look</option>
                      {% for cat in lookOptions %}
                          <option value="{{ cat.id }}" {{ look == cat.id ? 'selected '}}>{{ cat.title }}</option>
  
                      {% endfor %}
                  </select>
              </div>
              <div class="col-6 col-sm-4 col-md-3">
                  <select s-replace="#query_result" sprig name="finish" value="{{ finish }}" class="form-select w-100" aria-label="Finish">
                      <option value="" selected>Finish</option>
                      {% for cat in finishOptions %}
                          <option value="{{ cat.id }}" {{ finish == cat.id ? 'selected '}}>{{ cat.title }}</option>
  
                      {% endfor %}
                  </select>
              </div>
          </div>
      </div>
  </section>
  <section class="blck">
      <div class="container">
          <div class="row justify-content-center" id="query_result">
              {% if myQuery |length %}
                  {% for item in items %}
                      <div class="col-6 col-sm-4 col-md-3">
                          <div class="mb-5 img-item">
                              <a href="{{ item.url }}" class="img-ratio img-1-1">
                                  <div class="in">
                                      {% set image = item.projectImage[0] ?? null %}
                                      <img class="media" data-object-fit="cover" src="{{ image.url('collectionSubcatThumbnail')}}">
                                  </div>
                              </a>
                              <h6>{{ item.title }}</h6>
                              <a href="{{ item.url }}" aria-label="Discover {{ item.title }}" class="lnk-underline">Discover this stone</a>
                          </div>
                      </div>
                  {% endfor %}
              {% else %}
                  <div class="alert alert-warning">
                      <p class="mb-0">No products with that category or combination of categories. Try resetting the filters or deselecting 1 or more options.</p>
                  </div>
              {% endif %}
          </div>
      </div>
  </section>
  
  ```
  
  **Tags:** plugin-sprig, category-filter
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2021-06-15. Score: 1 | Accepted answer
  
  Turns out the error was elsewhere in the template where I had forgotten to add the .all() statement. The code in the example is error free.
  
  > **Comments:**
  > **username_1**: Can you mark this as the correct answer so that this question appears resolved?  
  
  ---
  
  ### Answer by username_2 on 2021-06-15. Score: 1
  
  One thing that stands out is that you're checking `myQuery | length` instead of `items | length`. Though I've tested it and this doesn't actually cause a deprecation error in my setup.
  
  Have you tried clearing the deprecation warnings in the backend manually? They won't disappear automatically if you change the template, but will stay visible until cleared manually. Try clearing them and reload the template, see if the warning appear again.
  
  
  ---
  
  