<p>You’re close! You just need to begin the array with <code>"and"</code>:</p>

<pre class="lang-php prettyprint-override"><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$criteria-&gt;customNumberField = array('and', '&gt;=5', '&lt;=10');

$users = $criteria-&gt;find();
</code></pre>

<p>This same thing could also be accomplished from your templates like this:</p>

<pre class="lang-twig prettyprint-override"><code>{% set users = craft.users.customNumberField('and', '&gt;=5', '&lt;=10') %}
</code></pre>

CC BY-SA 3.0
2014-08-07T10:29:02.843Z
Thanks! I had even tried it at the end but for some reason didn't try it at the start!
nfourtythree
CC BY-SA 3.0
2017-10-26T16:44:47.197Z
Brandon, how could/should you write your twig statement in a hash? For example I am using a hash to query the following: `{% set scriptsAboutToExpire = { section: 'scripts', order: 'expirationDate asc', expirationDate: 'and', '>=' ~ now, '<=' ~ now|date_modify('+2 weeks') } %}` but that obviously breaks because of the commas...
Steven Thate
CC BY-SA 3.0
2017-10-26T19:51:58.887Z
@StevenThate Just wrap the values in a sub-array – `expirationDate: ['and', '>='~now, '<='~now|date_modify('+2 weeks')]`
Brandon Kelly
CC BY-SA 3.0
2018-04-23T20:05:22.077Z
@BrandonKelly, how should your answer be expressed in Craft 3? I'm having some difficulty with it :( See: https://craftcms.stackexchange.com/questions/25825/craft-3-error-variable-now-does-not-exist
Steven Thate
CC BY-SA 3.0
2014-08-07T10:26:15.180Z
2014-08-07T10:26:15.180Z
Brandon Kelly
<p>I my be in a bit of a "wood for the trees" situation but how is it possible to return elements based on a custom field being between two supplied values.</p>

<p>For example returning users based on a number field. Let's say the number field is between 1 - 100 and I want to return users that have between 5 and 10 selected.</p>

<p>I could do it like the following</p>

<pre><code>$users = craft()-&gt;elements-&gt;getCriteria(ElementType::User, array("customNumberField"=&gt;array(5,6,7,8,9,10))-&gt;find();
</code></pre>

<p>However that sets <code>OR</code> </p>

<p>I know that you can use parameters eg <code>"customNumberField"=&gt;"&lt;= 10"</code> but obviously specifying that key twice causes it to be overwritten.</p>

<p>You can also specify an array with the parameters (avoiding the overwriting) like so</p>

<pre><code> $users = craft()-&gt;elements-&gt;getCriteria(ElementType::User, array("customNumberField"=&gt;array("&gt;= 5", "&lt;= 10"))-&gt;find();
</code></pre>

<p>But again this just creates <code>customNumberField &gt;= 5 OR customNumberField &lt;= 10</code>.</p>

<p>Is it possible to use the array as above but tell it to use <code>AND</code> instead of <code>OR</code>?</p>

<p>Am I on the right track or way off?</p>

<elementtype><elementcriteriamodel>
How can you return elements between two values?
# How can you return elements between two values?

(Asked by: username_0 on 2014-08-07)

I my be in a bit of a "wood for the trees" situation but how is it possible to return elements based on a custom field being between two supplied values.
For example returning users based on a number field. Let's say the number field is between 1 - 100 and I want to return users that have between 5 and 10 selected.
I could do it like the following
$users = craft()->elements->getCriteria(ElementType::User, array("customNumberField"=>array(5,6,7,8,9,10))->find();

However that sets OR 
I know that you can use parameters eg "customNumberField"=>"<= 10" but obviously specifying that key twice causes it to be overwritten.
You can also specify an array with the parameters (avoiding the overwriting) like so
 $users = craft()->elements->getCriteria(ElementType::User, array("customNumberField"=>array(">= 5", "<= 10"))->find();

But again this just creates customNumberField >= 5 OR customNumberField <= 10.
Is it possible to use the array as above but tell it to use AND instead of OR?
Am I on the right track or way off?

**Tags:** elementtype, elementcriteriamodel


---

## 1 answers

### Answer by username_1 on 2014-08-07. Score: 6 | Accepted answer

You’re close! You just need to begin the array with "and":
$criteria = craft()->elements->getCriteria(ElementType::User);
$criteria->customNumberField = array('and', '>=5', '<=10');

$users = $criteria->find();

This same thing could also be accomplished from your templates like this:
{% set users = craft.users.customNumberField('and', '>=5', '<=10') %}

> **Comments:**
> **username_0**: Thanks! I had even tried it at the end but for some reason didn't try it at the start!  
> **username_2**: Brandon, how could/should you write your twig statement in a hash? For example I am using a hash to query the following: `{% set scriptsAboutToExpire = { section: 'scripts', order: 'expirationDate asc', expirationDate: 'and', '>=' ~ now, '<=' ~ now|date_modify('+2 weeks') } %}` but that obviously breaks because of the commas...  
> **username_1**: @StevenThate Just wrap the values in a sub-array – `expirationDate: ['and', '>='~now, '<='~now|date_modify('+2 weeks')]`  
> **username_2**: @BrandonKelly, how should your answer be expressed in Craft 3? I'm having some difficulty with it :( See: https://craftcms.stackexchange.com/questions/25825/craft-3-error-variable-now-does-not-exist  

---

