<p>You need to retrieve the parameters from the current <a href="https://docs.craftcms.com/api/v4/craft-web-request.html" rel="nofollow noreferrer">request</a> and then apply them to the query. You can get the parameters like this:</p>
<pre class="lang-twig prettyprint-override"><code>{% set searchTerm = craft.app.request.getQueryParam('title') %}
{% set age = craft.app.request.getQueryParam('age') %}
</code></pre>
<p>The key is the <code>name</code> attribute in the input fields. You need to adjust your age input a bit so it can work properly:</p>
<ul>
<li>If you want to allow only one selected option for the <code>age</code> filter, use a <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio" rel="nofollow noreferrer">radio input</a> instead of checkboxes.</li>
<li>If you want to allow multiple selection for the <code>age</code> filter, change the name for each of the checkboxes to <code>age[]</code> so PHP treats the selected options as an array.</li>
</ul>
<p>Then you can apply the search term and the selected age to the query. Keep in mind that those parameters might be empty, so make sure to handle that case:</p>
<pre class="lang-twig prettyprint-override"><code>{% set productsQuery = craft.entries().section('products') %}

{% if searchTerm %}
    {% do productsQuery.search(searchTerm) %}
{% endif %}

{% if age %}
    {% do productsQuery.relatedTo(age) %}
{% endif %}

{% set products = productsQuery.all() %}
</code></pre>
<p>You can find all the available options for the search term in the <a href="https://craftcms.com/docs/4.x/searching.html" rel="nofollow noreferrer">documentation page on searching</a>.</p>
<p>The second filter assumes that your <code>age</code> input is the ID of a related entry (or an array of IDs). You can read more about filtering by related elements in the <a href="https://craftcms.com/docs/4.x/relations.html" rel="nofollow noreferrer">documentation page on relations</a>.</p>

CC BY-SA 4.0
2022-11-10T17:38:09.137Z
I ran into a problem. An entry from the section "products" has a field that consists of a set of checkboxes. How do I filter all available checkboxes? Moreover, I don't want all these flags to be displayed statically in the twig template. I don't want to edit them with pens later.
Dimi
CC BY-SA 4.0
2022-11-11T08:50:03.857Z
@Dimi I think the question regarding checkboxes is answered in your [other question](https://craftcms.stackexchange.com/questions/40116/how-do-i-get-a-list-of-checkboxes-for-the-item-addition-form) now? For the `age` filter, since the options are entries, you can use an [entry query](https://craftcms.com/docs/4.x/entries.html#querying-entries) to get all available options from the backend and use a loop to output them as checkboxes.
MoritzLost
CC BY-SA 4.0
2022-11-12T22:45:01.933Z
{%do products Query.related To(age) %} - this is if age is records from another section. But what if age is, for example, a checkboxes type field?
Dimi
CC BY-SA 4.0
2022-11-12T22:55:53.237Z
I thought like this: {% do products.relatedTo({sourceElement: age, field: 'ageField'}) %}
But my solution doesn't work. I suspect I'm doing something wrong.
Dimi
CC BY-SA 4.0
2022-11-14T10:43:06.320Z
@Dimi How you filter by a specific fields depends on the field type. For exampe, here's an example of filtering by a [Checkboxes field](https://craftcms.com/docs/4.x/checkboxes-fields.html#querying-elements-with-checkboxes-fields). If you're having trouble with that, please open a new question for that, as it's an entirely different issue from your original question. Please include the full code, any errors you're getting, the results you would like to see and what you're actually seeing, otherwise it's difficult to tell what's going wrong.
MoritzLost
CC BY-SA 4.0
2022-11-14T14:55:44.060Z
You're right. It would be necessary to create a new question. But in the last comment you understood what I mean and gave me the link I needed, which helped me. Next, I will create new questions.
Dimi
CC BY-SA 4.0
2022-11-18T18:01:46.373Z
I [created](https://craftcms.stackexchange.com/questions/40151/how-to-make-the-directory-filter-work-according-to-the-or-principle) a continuation of the filter question. Look pliz, maybe you know the answer
Dimi
CC BY-SA 4.0
2022-11-19T18:33:50.040Z
@Dimi I've provided an answer to that question. Though I would recommend you take a thorough look at the [documentation](https://craftcms.com/docs/4.x/), most of your questions are answered there.
MoritzLost
CC BY-SA 4.0
2022-11-09T15:01:52.587Z
2022-11-09T15:01:52.587Z
MoritzLost
<p>Do you guys have a simple example of how to make a filter of products on the site?</p>
<p>In the Twig template, I output a list of products like this:</p>
<pre class="lang-twig prettyprint-override"><code>{% set products = craft.entries().section('products').all() %}
{% for product in products%}
    &lt;tr&gt;
        &lt;td&gt;&lt;a href=&quot;/games/{{ product.slug }}&quot;&gt;{{ product.title }}&lt;/a&gt;&lt;/td&gt;
        &lt;td align=&quot;center&quot; width=&quot;100&quot;&gt;{{ product.price }}$&lt;/td&gt;
    &lt;/tr&gt;
{% endfor %}
</code></pre>
<p>In the same template, I have a filter form:</p>
<pre class="lang-twig prettyprint-override"><code>&lt;form method=&quot;GET&quot;&gt;
&lt;p&gt;
    Search by title:&lt;br /&gt;
    &lt;input type=&quot;text&quot; name=&quot;title&quot; value=&quot;&quot;&gt;
&lt;/p&gt;
&lt;p&gt;
    Check age:&lt;br /&gt;
    &lt;input type=&quot;checkbox&quot; name=&quot;age&quot; value=&quot;idElement1&quot;&gt;0-10&lt;br /&gt;
    &lt;input type=&quot;checkbox&quot; name=&quot;age&quot; value=&quot;idElement2&quot;&gt;10-25&lt;br /&gt;
    &lt;input type=&quot;checkbox&quot; name=&quot;age&quot; value=&quot;idElement3&quot;&gt;25-55&lt;br /&gt;
    &lt;input type=&quot;checkbox&quot; name=&quot;age&quot; value=&quot;idElement4&quot;&gt;55 - ...
&lt;/p&gt;
&lt;input type=&quot;submit&quot; value=&quot;go&quot;&gt;
&lt;/form&gt;
</code></pre>
<p>In my form, there is a title field, in which both the full name of the product and its partial name can be entered. For example, &quot;pampers&quot; or &quot;amper&quot;.
Also in the form, there are checkboxes in which you can select the age. In the &quot;age&quot; system, these are entries from another section.</p>
<p>As I understand it, this line: <code>{% set products = craft.entries().section('products').all() %}</code> should somehow change depending on what is specified in the URL parameters that will be generated after clicking the submit button in the filter form. But I can't figure out how to do it and how to do it better. Help please.</p>

<craft3>
How to create a filter of products?
# How to create a filter of products?

(Asked by: username_0 on 2022-11-09)

Do you guys have a simple example of how to make a filter of products on the site?
In the Twig template, I output a list of products like this:
{% set products = craft.entries().section('products').all() %}
{% for product in products%}
    
        {{ product.title }}
        {{ product.price }}$
    
{% endfor %}

In the same template, I have a filter form:


    Search by title:
    


    Check age:
    0-10
    10-25
    25-55
    55 - ...




In my form, there is a title field, in which both the full name of the product and its partial name can be entered. For example, "pampers" or "amper".
Also in the form, there are checkboxes in which you can select the age. In the "age" system, these are entries from another section.
As I understand it, this line: {% set products = craft.entries().section('products').all() %} should somehow change depending on what is specified in the URL parameters that will be generated after clicking the submit button in the filter form. But I can't figure out how to do it and how to do it better. Help please.

**Tags:** craft3

---

## 1 answers

### Answer by username_1 on 2022-11-09. Score: 2 | Accepted answer

You need to retrieve the parameters from the current request and then apply them to the query. You can get the parameters like this:
{% set searchTerm = craft.app.request.getQueryParam('title') %}
{% set age = craft.app.request.getQueryParam('age') %}

The key is the name attribute in the input fields. You need to adjust your age input a bit so it can work properly:

If you want to allow only one selected option for the age filter, use a radio input instead of checkboxes.
If you want to allow multiple selection for the age filter, change the name for each of the checkboxes to age[] so PHP treats the selected options as an array.

Then you can apply the search term and the selected age to the query. Keep in mind that those parameters might be empty, so make sure to handle that case:
{% set productsQuery = craft.entries().section('products') %}

{% if searchTerm %}
    {% do productsQuery.search(searchTerm) %}
{% endif %}

{% if age %}
    {% do productsQuery.relatedTo(age) %}
{% endif %}

{% set products = productsQuery.all() %}

You can find all the available options for the search term in the documentation page on searching.
The second filter assumes that your age input is the ID of a related entry (or an array of IDs). You can read more about filtering by related elements in the documentation page on relations.

> **Comments:**
> **username_0**: I ran into a problem. An entry from the section "products" has a field that consists of a set of checkboxes. How do I filter all available checkboxes? Moreover, I don't want all these flags to be displayed statically in the twig template. I don't want to edit them with pens later.  
> **username_1**: @Dimi I think the question regarding checkboxes is answered in your [other question](https://craftcms.stackexchange.com/questions/40116/how-do-i-get-a-list-of-checkboxes-for-the-item-addition-form) now? For the `age` filter, since the options are entries, you can use an [entry query](https://craftcms.com/docs/4.x/entries.html#querying-entries) to get all available options from the backend and use a loop to output them as checkboxes.  
> **username_0**: {%do products Query.related To(age) %} - this is if age is records from another section. But what if age is, for example, a checkboxes type field?  
> **username_0**: I thought like this: {% do products.relatedTo({sourceElement: age, field: 'ageField'}) %} But my solution doesn't work. I suspect I'm doing something wrong.  
> **username_1**: @Dimi How you filter by a specific fields depends on the field type. For exampe, here's an example of filtering by a [Checkboxes field](https://craftcms.com/docs/4.x/checkboxes-fields.html#querying-elements-with-checkboxes-fields). If you're having trouble with that, please open a new question for that, as it's an entirely different issue from your original question. Please include the full code, any errors you're getting, the results you would like to see and what you're actually seeing, otherwise it's difficult to tell what's going wrong.  
> **username_0**: You're right. It would be necessary to create a new question. But in the last comment you understood what I mean and gave me the link I needed, which helped me. Next, I will create new questions.  
> **username_0**: I [created](https://craftcms.stackexchange.com/questions/40151/how-to-make-the-directory-filter-work-according-to-the-or-principle) a continuation of the filter question. Look pliz, maybe you know the answer  
> **username_1**: @Dimi I've provided an answer to that question. Though I would recommend you take a thorough look at the [documentation](https://craftcms.com/docs/4.x/), most of your questions are answered there.  

---

