Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-04T13:01:21.493Z
Answers.Text: @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.
Answers.UserDisplayName: James Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-04T13:06:12.467Z
Answers.Text: @JamesSmith That's a valid remark. Do you have any references on how to do this?
Answers.UserDisplayName: Matthias
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-09-04T12:23:59.977Z
Answers.LastActivityDate: 2023-09-04T12:23:59.977Z
Answers.UserDisplayName: Ben Croker
Body: <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>
  
Tags: <templating><craft4><plugin-sprig>
Title: Create deep copy of twig variable
ThreadText: # 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`](https://craftcms.com/docs/4.x/dev/functions.html#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**: @username_2 That's a valid remark. Do you have any references on how to do this?  
  
  ---
  
  