<p>You don't filter a <code>loop</code> but you can attach certain criteria in your <code>ElementQuery</code>. <code>craft.entries</code> returns you an object of type <code>EntryQuery</code> (<a href="https://github.com/craftcms/docs/blob/v3/en/element-queries.md" rel="nofollow noreferrer">link to docs</a>). You can search for specific entries based on different criterias by calling magic methods or function in your <code>Query</code> object.</p>

<p>For example: <code>section('docs')</code> will return only entries in the section <code>docs</code>. You can attach any field handle that is in your field layout as well.. In your case <code>account('05')</code>.</p>

<pre><code>{% for line in craft.entries.section('docs').account('05').all() %}
     &lt;a href="{{ line.url }}"&gt;{{line.lineItemTitle}} ({{line.lineItemTitle}})&lt;/a&gt;, 
{% endfor %}
</code></pre>

<p>If you want to search for multiple values you can include them as array or comma separated string.</p>

<pre><code>craft.entries
    .section(['section1', 'section2')
    .account('and, &gt;=5, &lt;=10')
    .anotherField('or, 3, 5')
    .someThingElse(['or' ,3 , 5])
    .all()
</code></pre>

<p>You can see all available syntax examples in the <a href="https://docs.craftcms.com/api/v3/craft-helpers-db.html#parseParam()-detail" rel="nofollow noreferrer">DB::parseParam()</a> function</p>

CC BY-SA 3.0
2018-04-14T16:01:01.237Z
2018-04-14T16:08:04.513Z
2018-04-14T16:08:04.513Z
Robin Schambach
<p>How can I filter a <code>for loop</code> based on the value of a field?</p>

<p>I tried <code>filterByValue</code>, but I do not know how to construct the statement.</p>

<p>This is NOT a category field. </p>

<p>I have this...</p>

<pre><code>{% for line in craft.entries.section('docs').filterByVale('account: 05').all() %}
    &lt;a href="{{ line.url }}"&gt;{{line.lineItemTitle}} ({{line.lineItemTitle}})&lt;/a&gt;, 
{% endfor %}
</code></pre>

<p>How do I do this?</p>

<p>Thanks</p>

<templating><elementquery>
How can I filter a for loop based on the value of a field?
# How can I filter a for loop based on the value of a field?

(Asked by: username_0 on 2018-04-14)

How can I filter a for loop based on the value of a field?
I tried filterByValue, but I do not know how to construct the statement.
This is NOT a category field. 
I have this...
{% for line in craft.entries.section('docs').filterByVale('account: 05').all() %}
    {{line.lineItemTitle}} ({{line.lineItemTitle}}), 
{% endfor %}

How do I do this?
Thanks

**Tags:** templating, elementquery


---

## 1 answers

### Answer by username_1 on 2018-04-14. Score: 2

You don't filter a loop but you can attach certain criteria in your ElementQuery. craft.entries returns you an object of type EntryQuery (link to docs). You can search for specific entries based on different criterias by calling magic methods or function in your Query object.
For example: section('docs') will return only entries in the section docs. You can attach any field handle that is in your field layout as well.. In your case account('05').
{% for line in craft.entries.section('docs').account('05').all() %}
     {{line.lineItemTitle}} ({{line.lineItemTitle}}), 
{% endfor %}

If you want to search for multiple values you can include them as array or comma separated string.
craft.entries
    .section(['section1', 'section2')
    .account('and, >=5, <=10')
    .anotherField('or, 3, 5')
    .someThingElse(['or' ,3 , 5])
    .all()

You can see all available syntax examples in the DB::parseParam() function


---

