Answers.Body: <p>That's a lot of code to post in a question!</p>
  <p>The basic gist is that instead of merging new related params with <code>relatedParams|merge</code> and <em>then</em> applying to the query, you can call <code>andRelatedTo(newParams)</code> directly on the query.</p>
  <pre><code>{% set productsQuery = craft.products %}
  {% set someEntryId = craft.app.request.queryParam('someEntryId') %}
  
  {# BEFORE #}
  {% if someEntryId %}
      {% set relatedParams = relatedParams|merge([someEntryId]) %}
  {% endif %}
  {% set productsQuery = productsQuery.relatedTo(relatedParams) %}
  
  {# AFTER #}
  {% if someEntryId %}
      {% do productsQuery.andRelatedTo(someEntryId) %}
  {% endif %}
  </code></pre>
  <p>I explain this in more detail in the <a href="https://putyourlightson.com/sprig/videos#sprig-filters" rel="nofollow noreferrer">Sprig Filters</a> video series.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-22T07:29:16.723Z
Answers.Text: Can you mark this as the correct answer, assuming you are satisfied with it, so that this question appears resolved?
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-13T06:37:49.733Z
Answers.LastActivityDate: 2021-08-13T06:37:49.733Z
Answers.UserDisplayName: Ben Croker
Body: <p>I have the following code that works with Sprig. But with the new andRelatedTo param, I am wondering if this could be simplified? However, I am a little unsure if/where to start on this and how the new <code>andRelatedTo</code> would replace existing code.</p>
  <p>Can anyone steer me in the right direction?</p>
  <pre><code>{% set defaultLimit = 35 %}
  {% set search = search ?? '' %}
  {% set sort = sort ?? 'title ASC' %}
  {% set limit = limit ?? craft.app.request.queryParam('limit') ?? defaultLimit %}
  {% set page = page ?? 1 %}
  {% set category = craft.categories.id(_categoryId).one() %}
  
  {# {% set alllowedEntryTypeHandles = ['sponsored', 'free'] %} #}
  {% set limitOptions = [2, 5, 10, 25, 35, 50, 100, 'All'] %}
  {% set limit = limit in limitOptions ? limit : defaultLimit %}
  
  {% set params = {
    limit:limit,
    page: page,
  } %}
  
  {% set relatedParams = ['and'] %}
  {% set relatedParams = relatedParams|merge([]) %}
  {% set filterIds = '' %}
  
  {% set pcdQueryString = null %}
  {% set cbQueryString = null %}
  {% set finishQueryString = null %}
  
  {% if craft.app.request.queryParam('pcd') %}
    {% set pcdQueryString = craft.app.request.queryParam('pcd') | join(',') %}
    {% set filterIds = filterIds ~ ',' ~ pcdQueryString|join(',') %}
    {% set relatedParams = relatedParams|merge([pcdQueryString]) %}
    {% set params = params|merge({pcd:pcdQueryString}) %}
  {% endif %}
  
  {% if craft.app.request.queryParam('cb') %}
    {% set cbQueryString = craft.app.request.queryParam('cb') | join(',') %}
    {% set filterIds = filterIds ~ ',' ~ cbQueryString|join(',') %}
    {% set relatedParams = relatedParams|merge([cbQueryString]) %}
    {% set params = params|merge({cb:cbQueryString}) %}
  {% endif %}
  
  {% if craft.app.request.queryParam('finish') %}
    {% set finishQueryString = craft.app.request.queryParam('finish') | join(',') %}
    {% set filterIds = filterIds ~ ',' ~ finishQueryString|join(',') %}
    {% set relatedParams = relatedParams|merge([finishQueryString]) %}
    {% set params = params|merge({finish:finishQueryString}) %}
  {% endif %}
  
  {# SAERCH #}
  {% if craft.app.request.queryParam('search') %}
    {% set params = params|merge({search:search}) %}
  {% endif %}
  
   {% set filterTags = craft.entries
     .id(filterIds)
     .orderBy('title ASC')
     .all() %}
  
  {% set allProductsQuery = craft.products
    .relatedTo(category)
    .all() %}
  
   {% set productsQuery = craft.products
     .search(search)
     .with( [
       'department',
       'productImage',
       'finish',
       'pcd',
       'variants',
       'thread',
       'seat',
       'hexHead'
     ])
     .orderBy(sort)
     .limit(limit)
     .relatedTo(relatedParams) %}
  
  {% do sprig.pushUrl('?' ~ params|url_encode) %}
  </code></pre>
  
Tags: <relations><plugin-sprig>
Title: Simplifying code using the new andRelatedTo param
ThreadText: # Simplifying code using the new andRelatedTo param
  
  (Asked by: username_0 on 2021-04-26)
  
  I have the following code that works with Sprig. But with the new andRelatedTo param, I am wondering if this could be simplified? However, I am a little unsure if/where to start on this and how the new `andRelatedTo` would replace existing code.
  
  Can anyone steer me in the right direction?
  
  ```
  {% set defaultLimit = 35 %}
  {% set search = search ?? '' %}
  {% set sort = sort ?? 'title ASC' %}
  {% set limit = limit ?? craft.app.request.queryParam('limit') ?? defaultLimit %}
  {% set page = page ?? 1 %}
  {% set category = craft.categories.id(_categoryId).one() %}
  
  {# {% set alllowedEntryTypeHandles = ['sponsored', 'free'] %} #}
  {% set limitOptions = [2, 5, 10, 25, 35, 50, 100, 'All'] %}
  {% set limit = limit in limitOptions ? limit : defaultLimit %}
  
  {% set params = {
    limit:limit,
    page: page,
  } %}
  
  {% set relatedParams = ['and'] %}
  {% set relatedParams = relatedParams|merge([]) %}
  {% set filterIds = '' %}
  
  {% set pcdQueryString = null %}
  {% set cbQueryString = null %}
  {% set finishQueryString = null %}
  
  {% if craft.app.request.queryParam('pcd') %}
    {% set pcdQueryString = craft.app.request.queryParam('pcd') | join(',') %}
    {% set filterIds = filterIds ~ ',' ~ pcdQueryString|join(',') %}
    {% set relatedParams = relatedParams|merge([pcdQueryString]) %}
    {% set params = params|merge({pcd:pcdQueryString}) %}
  {% endif %}
  
  {% if craft.app.request.queryParam('cb') %}
    {% set cbQueryString = craft.app.request.queryParam('cb') | join(',') %}
    {% set filterIds = filterIds ~ ',' ~ cbQueryString|join(',') %}
    {% set relatedParams = relatedParams|merge([cbQueryString]) %}
    {% set params = params|merge({cb:cbQueryString}) %}
  {% endif %}
  
  {% if craft.app.request.queryParam('finish') %}
    {% set finishQueryString = craft.app.request.queryParam('finish') | join(',') %}
    {% set filterIds = filterIds ~ ',' ~ finishQueryString|join(',') %}
    {% set relatedParams = relatedParams|merge([finishQueryString]) %}
    {% set params = params|merge({finish:finishQueryString}) %}
  {% endif %}
  
  {# SAERCH #}
  {% if craft.app.request.queryParam('search') %}
    {% set params = params|merge({search:search}) %}
  {% endif %}
  
   {% set filterTags = craft.entries
     .id(filterIds)
     .orderBy('title ASC')
     .all() %}
  
  {% set allProductsQuery = craft.products
    .relatedTo(category)
    .all() %}
  
   {% set productsQuery = craft.products
     .search(search)
     .with( [
       'department',
       'productImage',
       'finish',
       'pcd',
       'variants',
       'thread',
       'seat',
       'hexHead'
     ])
     .orderBy(sort)
     .limit(limit)
     .relatedTo(relatedParams) %}
  
  {% do sprig.pushUrl('?' ~ params|url_encode) %}
  
  ```
  
  **Tags:** relations, plugin-sprig
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-08-13. Score: 3
  
  That's a lot of code to post in a question!
  
  The basic gist is that instead of merging new related params with `relatedParams|merge` and *then* applying to the query, you can call `andRelatedTo(newParams)` directly on the query.
  
  ```
  {% set productsQuery = craft.products %}
  {% set someEntryId = craft.app.request.queryParam('someEntryId') %}
  
  {# BEFORE #}
  {% if someEntryId %}
      {% set relatedParams = relatedParams|merge([someEntryId]) %}
  {% endif %}
  {% set productsQuery = productsQuery.relatedTo(relatedParams) %}
  
  {# AFTER #}
  {% if someEntryId %}
      {% do productsQuery.andRelatedTo(someEntryId) %}
  {% endif %}
  
  ```
  
  I explain this in more detail in the [Sprig Filters](https://putyourlightson.com/sprig/videos#sprig-filters) video series.
  
  > **Comments:**
  > **username_1**: Can you mark this as the correct answer, assuming you are satisfied with it, so that this question appears resolved?  
  
  ---
  
  