3

How do you add extra parameters or update a parameter to an exiting POST URL?

ADD EXTRA PARAMETER

For example, I have a page at this address:

http://localhost/shop/?color=red&order=title

And in my twig I want to add a parameter like this:

http://localhost/shop/?color=red&order=title&sort=asc
http://localhost/shop/?color=red&order=title&sort=asc&limit=10

UPDATE PARAMETER

For example, I have a page at this address:

http://localhost/shop/?color=red&order=title

And in my twig I want to update a parameter like this:

http://localhost/shop/?color=blue&order=title
http://localhost/shop/?color=red&order=price

1 Answer 1

4

If i'm not mistaking, craft.request.getQuery() returns an array filled with all $_GET key-value pairs, so in your case:

{% set queryParams = craft.request.getQuery() %}

will let you do:

// Do with it what you like ...
{% set queryParams = queryParams | without(key) %} // Removes a param
{% set queryParams = queryParams | merge([{ 'limit' : 10 }]) %} // Adds a param
{% set queryParams['order'] = 'price' %} // Changes a param

After doing what you need to do, you can rebuild the query string:

{% set queryString = '' %}
{% for key, value in queryParams %}
    {% set queryString = loop.first ? '?' ~ key ~ '=' ~ value : '&' ~ key ~ '=' ~ value %}
{% endfor %}
{% redirect(siteUrl ~ queryString) %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.