1

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() %}
    <a href="{{ line.url }}">{{line.lineItemTitle}} ({{line.lineItemTitle}})</a>, 
{% endfor %}

How do I do this?

Thanks

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() %}
     <a href="{{ line.url }}">{{line.lineItemTitle}} ({{line.lineItemTitle}})</a>, 
{% 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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