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

CC BY-SA 4.0
2021-08-22T07:27:49.373Z
Can you mark this as the correct answer so that this question appears resolved?
Ben Croker
CC BY-SA 4.0
2021-06-15T10:39:45.487Z
2021-06-15T10:39:45.487Z
erwinheiser
<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>

CC BY-SA 4.0
2021-06-15T07:19:32.513Z
2021-06-15T07:19:32.513Z
MoritzLost
<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>

<plugin-sprig><category-filter>
Using sprig for category filter but keep getting deprecation errors
# 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() %}



    
        
            
                
                    
                        reset filters
                        
                            
                            
                        
                    
                
            
            
                
                    Thickness
                    {% for cat in thicknessOptions %}
                        {{ cat.title }}
                    {% endfor %}
                
            
            
                
                    Look
                    {% for cat in lookOptions %}
                        {{ cat.title }}

                    {% endfor %}
                
            
            
                
                    Finish
                    {% for cat in finishOptions %}
                        {{ cat.title }}

                    {% endfor %}
                
            
        
    


    
        
            {% if myQuery |length %}
                {% for item in items %}
                    
                        
                            
                                
                                    {% set image = item.projectImage[0] ?? null %}
                                    
                                
                            
                            {{ item.title }}
                            Discover this stone
                        
                    
                {% endfor %}
            {% else %}
                
                    No products with that category or combination of categories. Try resetting the filters or deselecting 1 or more options.
                
            {% endif %}

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


---

