<p>The <a href="https://craftcms.com/docs/4.x/dev/functions.html#clone" rel="nofollow noreferrer"><code>clone</code></a> function makes it possible to clone a query for multiple use.</p>
<pre><code>{% set query = craft.products() %}
{% set products = clone(query).andRelatedTo(colours) %}
</code></pre>
<p>That answers the question, although I’m not sure it solves the underlying issue. If you want to check for <em>any</em> related colours, then I’d suggest passing an array into <code>andRelatedTo</code>.</p>

CC BY-SA 4.0
2023-09-04T13:01:21.493Z
@Matthias Keep a close eye on your query count/performance with this approach, as you're going to be making separate queries for every checkbox. If you end up with lots of filters the performance will suffer. Consider using a look-up table/hash map with all the values you want.
James Smith
CC BY-SA 4.0
2023-09-04T13:06:12.467Z
@JamesSmith That's a valid remark. Do you have any references on how to do this?
Matthias
CC BY-SA 4.0
2023-09-04T12:23:59.977Z
2023-09-04T12:23:59.977Z
Ben Croker
<p>I'm building a filter page with Craft and Sprig.
The goal is to show the amount of products for each filter.
So for example if we want to filter on color we show 2 checkboxes &quot;Red&quot; and &quot;Green&quot; and I want to show the number of related products for the Red items and the related number for the green number.</p>
<p>The problem I'm currently encountering is that I calculate the amount of results by adding a <code>andRelatedTo</code> parameter to my main query. This works perfect for the first checkbox but since the second checkbox adds another <code>andRelatedTo</code> parameter and the previous one is still there the number will always be 0 because none of the products are both &quot;Red&quot; and &quot;Green&quot;.</p>
<p>Is there a way to create a deep copy of the original main query so I can reuse it without polluting it with the count of the available options?</p>

<templating><craft4><plugin-sprig>
Create deep copy of twig variable
# Create deep copy of twig variable

(Asked by: username_0 on 2023-09-04)

I'm building a filter page with Craft and Sprig.
The goal is to show the amount of products for each filter.
So for example if we want to filter on color we show 2 checkboxes "Red" and "Green" and I want to show the number of related products for the Red items and the related number for the green number.

The problem I'm currently encountering is that I calculate the amount of results by adding a `andRelatedTo` parameter to my main query. This works perfect for the first checkbox but since the second checkbox adds another `andRelatedTo` parameter and the previous one is still there the number will always be 0 because none of the products are both "Red" and "Green".

Is there a way to create a deep copy of the original main query so I can reuse it without polluting it with the count of the available options?

**Tags:** templating, craft4, plugin-sprig


---

## 1 answers

### Answer by username_1 on 2023-09-04. Score: 2 | Accepted answer

The clone function makes it possible to clone a query for multiple use.
{% set query = craft.products() %}
{% set products = clone(query).andRelatedTo(colours) %}

That answers the question, although I’m not sure it solves the underlying issue. If you want to check for any related colours, then I’d suggest passing an array into andRelatedTo.

> **Comments:**
> **username_2**: @username_0 Keep a close eye on your query count/performance with this approach, as you're going to be making separate queries for every checkbox. If you end up with lots of filters the performance will suffer. Consider using a look-up table/hash map with all the values you want.  
> **username_0**: @JamesSmith That's a valid remark. Do you have any references on how to do this?  

---

