<p>The documentation for every field type includes examples for sorting and filtering by that field. Here's the <a href="https://craftcms.com/docs/4.x/number-fields.html" rel="nofollow noreferrer">documentation for the numbers field</a> which includes examples for this exact scenario.</p>
<p>If there's only a minimum or a maximum value, you can filter the price with the <code>&gt;=</code> or <code>&lt;=</code> operator, respectively:</p>
<pre class="lang-twig prettyprint-override"><code>{% if min %}
    {% do games.price(&quot;&gt;= #{min}&quot;) %}
{% endif %}
</code></pre>
<p>However, if there's both a minimum <em>and</em> a maximum value, you can't add both filters separately, because the second one will overwrite the first one. In this case, you need to use the array syntax as demonstrated in the documentation linked above:</p>
<pre class="lang-twig prettyprint-override"><code>{% if min and max %}
    {% do games.price(['AND', &quot;&gt;= #{min}&quot;, &quot;&lt;= #{max}&quot;]) %}
{% endif %}
</code></pre>
<p>In both examples, replace <code>price</code> with the handle of your numeric price field.</p>
<blockquote>
<p>How do I find out the price of the most expensive and minimal game?</p>
</blockquote>
<p>To find the most and least expensive game, use an entry query and sort by price. Then get the first/last item from the list using <a href="https://craftcms.com/docs/4.x/element-queries.html#one" rel="nofollow noreferrer"><code>.one()</code></a> and take the price from that entry:</p>
<pre class="lang-twig prettyprint-override"><code>{% set leastExpensiveGame = craft.entries()
        .section('games')
        .orderBy('price ASC')
        .one()
%}
{% set minimumPrice = leastExpensiveGame.price %}
</code></pre>
<p>Replace <code>price</code> with the handle of your price field. For the most expensive game, use another query with the same parameters, but sort by <code>price DESC</code> (descending).</p>

CC BY-SA 4.0
2022-11-17T12:52:51.513Z
Thx for answer. But i still don't understand how to get price of the most expensive or minimal game from my catalog?
Dimi
CC BY-SA 4.0
2022-11-17T13:52:26.580Z
I came up with just such a solution: `{% set max = craft.entries().section('games').orderBy('priceBaz DESC').one() %}` But I do not know how much it is correct
Dimi
CC BY-SA 4.0
2022-11-18T16:14:52.673Z
@Dimi Ah, I missed that part of the question. I've updated my answer with some code to get the least/most expensive item.
MoritzLost
CC BY-SA 4.0
2022-11-18T17:45:19.567Z
Well, in principle, I thought that you can search for the maximum value through ORDER. However, I thought there was a solution of something like:
`set max = craft.entries().section('games').getValue('priceField').max()`
Dimi
CC BY-SA 4.0
2022-11-19T18:19:13.200Z
@Dimi There's more than one way to skin a cat. Using two queries with ASC/DESC order is perfectly fine. If you want to further optimize the queries, use `select()` to limit the results to one particular field (see [this question](https://craftcms.stackexchange.com/q/38471/11705) and all the answers for details) and execute the query with [`.scalar()`](https://www.yiiframework.com/doc/api/2.0/yii-db-query#scalar()-detail) to get the price as a single scalar value. But I wouldn't spend my time worrying about theoretical performance bottlenecks until you actually hit one.
MoritzLost
CC BY-SA 4.0
2022-11-17T11:03:57.410Z
2022-11-18T15:20:29.193Z
2022-11-18T15:20:29.193Z
MoritzLost
<p>I have a catalog of games. Every game has its own price. The price is a numeric field.</p>
<p>At the moment I'm building a game catalog filter, and now I'm stuck on implementing the ability to filter products by price. There are two input in the filter form: maximum and minimum price fields.</p>
<pre><code>    &lt;div&gt;
        Search by price:&lt;br /&gt;
        from:&lt;input name=&quot;min&quot; type=&quot;text&quot; value=&quot;&quot;&gt;
        to:&lt;input name=&quot;max&quot; type=&quot;text&quot; value=&quot;&quot;&gt;
    &lt;/div&gt;
</code></pre>
<p>By filling in these two fields, the user needs to show the games within the selected price.
A few questions, the answers to which will help me get closer to the solution:</p>
<p>1.How do I find out the price of the most expensive and minimal game?</p>
<p>2.This is how in the twig template I check for the presence of the minprice or maxprice parameter in the url.</p>
<pre><code>{% set games = craft.entries().section('games') %}

{% set minprice = craft.app.request.getQueryParam('min') %}
{% if minprice %}
    {% do .................. %}
{% endif %}

{% set maxprice = craft.app.request.getQueryParam('max') %}
{% if maxprice %}
    {% do .................. %}
{% endif %}
</code></pre>
<p>I do not know what to write in my code instead of dots. Help me please with this))</p>

<craft3>
How do I filter items by price?
# How do I filter items by price?

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

I have a catalog of games. Every game has its own price. The price is a numeric field.
At the moment I'm building a game catalog filter, and now I'm stuck on implementing the ability to filter products by price. There are two input in the filter form: maximum and minimum price fields.
    
        Search by price:
        from:
        to:
    

By filling in these two fields, the user needs to show the games within the selected price.
A few questions, the answers to which will help me get closer to the solution:
1.How do I find out the price of the most expensive and minimal game?
2.This is how in the twig template I check for the presence of the minprice or maxprice parameter in the url.
{% set games = craft.entries().section('games') %}

{% set minprice = craft.app.request.getQueryParam('min') %}
{% if minprice %}
    {% do .................. %}
{% endif %}

{% set maxprice = craft.app.request.getQueryParam('max') %}
{% if maxprice %}
    {% do .................. %}
{% endif %}

I do not know what to write in my code instead of dots. Help me please with this))

**Tags:** craft3


---

## 1 answers

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

The documentation for every field type includes examples for sorting and filtering by that field. Here's the documentation for the numbers field which includes examples for this exact scenario.
If there's only a minimum or a maximum value, you can filter the price with the >= or <= operator, respectively:
{% if min %}
    {% do games.price(">= #{min}") %}
{% endif %}

However, if there's both a minimum and a maximum value, you can't add both filters separately, because the second one will overwrite the first one. In this case, you need to use the array syntax as demonstrated in the documentation linked above:
{% if min and max %}
    {% do games.price(['AND', ">= #{min}", "<= #{max}"]) %}
{% endif %}

In both examples, replace price with the handle of your numeric price field.

How do I find out the price of the most expensive and minimal game?

To find the most and least expensive game, use an entry query and sort by price. Then get the first/last item from the list using .one() and take the price from that entry:
{% set leastExpensiveGame = craft.entries()
        .section('games')
        .orderBy('price ASC')
        .one()
%}
{% set minimumPrice = leastExpensiveGame.price %}

Replace price with the handle of your price field. For the most expensive game, use another query with the same parameters, but sort by price DESC (descending).

> **Comments:**
> **username_0**: Thx for answer. But i still don't understand how to get price of the most expensive or minimal game from my catalog?  
> **username_0**: I came up with just such a solution: `{% set max = craft.entries().section('games').orderBy('priceBaz DESC').one() %}` But I do not know how much it is correct  
> **username_1**: @username_0 Ah, I missed that part of the question. I've updated my answer with some code to get the least/most expensive item.  
> **username_0**: Well, in principle, I thought that you can search for the maximum value through ORDER. However, I thought there was a solution of something like: `set max = craft.entries().section('games').getValue('priceField').max()`  
> **username_1**: @username_0 There's more than one way to skin a cat. Using two queries with ASC/DESC order is perfectly fine. If you want to further optimize the queries, use `select()` to limit the results to one particular field (see [this question](https://craftcms.stackexchange.com/q/38471/11705) and all the answers for details) and execute the query with [`.scalar()`](https://www.yiiframework.com/doc/api/2.0/yii-db-query#scalar()-detail) to get the price as a single scalar value. But I wouldn't spend my time worrying about theoretical performance bottlenecks until you actually hit one.  

---

