<p>So I'd approach it something like this:</p>
<pre><code>    {# Get the incoming query params #}
    {% set params = craft.app.request.queryParams %}
    {# Change/add the params we need to #}
    {% set params = params | merge({
        'vintage' : 4
    }) %}
    {# Map the array into a query string #}
    {% set queryString = params | url_encode %}
    {# Output our link #}
    &lt;a href=&quot;{{ url('wine') }}?{{ queryString }}&quot;&gt;Link&lt;/a&gt;
</code></pre>
<p>Basically, get an array of query parameters from the incoming request, then modify or add the one(s) we want to change, and re-ouput the query string.</p>
<p>The benefit of this method is that it'll preserve whatever is already in the query string, including things like <code>gclid</code> or other tracking parameters.</p>

CC BY-SA 4.0
2022-08-13T13:23:08.933Z
You can replace the `|map()` filter with `|url_encode` to simplify the solution. Also, `{ url('wine' ~ '?' ~ queryString) }}` might read cleaner.
Ben Croker
CC BY-SA 4.0
2022-08-13T15:33:54.517Z
Ah nice, forgot about the `url_encode` filter! https://twig.symfony.com/doc/3.x/filters/url_encode.html
andrew.welch
CC BY-SA 4.0
2022-08-16T21:02:27.093Z
This is somewhat working. What I'm finding is that for each category group/loop, the last item in the loop is used in the next loop's query string. Using the same initial example, the last link in the country set/group is set to `/wine?country=54 ` (others have their corresponding IDs) & all links in the varietal group have the country param set even though the url may have the country param set to 50 (if it's present in the url at all). Hope that makes sense.
A Herrera
CC BY-SA 4.0
2022-08-16T21:06:19.917Z
Here's a screenshot in case it helps to demonstrate the issue: https://share.cleanshot.com/31FoZf
A Herrera
CC BY-SA 4.0
2022-08-26T19:15:06.220Z
I'd need to see your code for context to help further. Can you provide some in the original question that is updated with what you're doing now?
andrew.welch
CC BY-SA 4.0
2022-08-12T23:13:48.743Z
2022-08-13T18:08:49.003Z
2022-08-13T18:08:49.003Z
andrew.welch
<p>I have code similar to below that basically just outputs links with query parameters:</p>
<pre><code>{% for country in craft.categories.group(&quot;country&quot;).all() %}
   &lt;a href=&quot;{{ url('wine') }}?country={{ country.id }}&quot;&gt;{{ country.title }}&lt;/a&gt;
{% endfor %}

{% for varietal in craft.categories.group(&quot;varietal&quot;).all() %}
   &lt;a href=&quot;{{ url('wine') }}?varietal={{ varietal.id }}&quot;&gt;{{ varietal.title }}&lt;/a&gt;
{% endfor %}

{% for vintage in craft.categories.group(&quot;vintage&quot;).all() %}
   &lt;a href=&quot;{{ url('wine') }}?vintage={{ vintage.id }}&quot;&gt;{{ vintage.title }}&lt;/a&gt;
{% endfor %}
</code></pre>
<p>This is all fine. Clicking on a link gives me what I need. For instance, clicking on a country link, I get this URL: <code>site.com/wine?country=1</code></p>
<p>Here's where I'm stuck: how do I go about adding to the query string when clicking on multiple links not in the same category group. In other words if I click on a country link and a varietal link right after, I want the URL to be updated to look like so: <code>site.com/wine?country=1&amp;varietal=2</code></p>
<p>Then, if I click on a third link, such as vintage, update the URL like so: <code>site.com/wine?country=1&amp;varietal=2&amp;vintage=3</code></p>
<p>But then, if I click on, say a different vintage link, just update the value rather than adding to the string further, like so: <code>site.com/wine?country=1&amp;varietal=2&amp;vintage=99</code></p>

<templating><querystring>
Updating/replacing query string parameters with twig
# Updating/replacing query string parameters with twig

(Asked by: username_0 on 2022-08-12)

I have code similar to below that basically just outputs links with query parameters:
{% for country in craft.categories.group("country").all() %}
   {{ country.title }}
{% endfor %}

{% for varietal in craft.categories.group("varietal").all() %}
   {{ varietal.title }}
{% endfor %}

{% for vintage in craft.categories.group("vintage").all() %}
   {{ vintage.title }}
{% endfor %}

This is all fine. Clicking on a link gives me what I need. For instance, clicking on a country link, I get this URL: site.com/wine?country=1
Here's where I'm stuck: how do I go about adding to the query string when clicking on multiple links not in the same category group. In other words if I click on a country link and a varietal link right after, I want the URL to be updated to look like so: site.com/wine?country=1&varietal=2
Then, if I click on a third link, such as vintage, update the URL like so: site.com/wine?country=1&varietal=2&vintage=3
But then, if I click on, say a different vintage link, just update the value rather than adding to the string further, like so: site.com/wine?country=1&varietal=2&vintage=99

**Tags:** templating, querystring


---

## 1 answers

### Answer by username_1 on 2022-08-12. Score: 1

So I'd approach it something like this:
    {# Get the incoming query params #}
    {% set params = craft.app.request.queryParams %}
    {# Change/add the params we need to #}
    {% set params = params | merge({
        'vintage' : 4
    }) %}
    {# Map the array into a query string #}
    {% set queryString = params | url_encode %}
    {# Output our link #}
    Link

Basically, get an array of query parameters from the incoming request, then modify or add the one(s) we want to change, and re-ouput the query string.
The benefit of this method is that it'll preserve whatever is already in the query string, including things like gclid or other tracking parameters.

> **Comments:**
> **username_2**: You can replace the `|map()` filter with `|url_encode` to simplify the solution. Also, `{ url('wine' ~ '?' ~ queryString) }}` might read cleaner.  
> **username_1**: Ah nice, forgot about the `url_encode` filter! https://twig.symfony.com/doc/3.x/filters/url_encode.html  
> **username_0**: This is somewhat working. What I'm finding is that for each category group/loop, the last item in the loop is used in the next loop's query string. Using the same initial example, the last link in the country set/group is set to `/wine?country=54 ` (others have their corresponding IDs) & all links in the varietal group have the country param set even though the url may have the country param set to 50 (if it's present in the url at all). Hope that makes sense.  
> **username_0**: Here's a screenshot in case it helps to demonstrate the issue: https://share.cleanshot.com/31FoZf  
> **username_1**: I'd need to see your code for context to help further. Can you provide some in the original question that is updated with what you're doing now?  

---

