<p><strong>Update</strong>
I may have found a better option for you. Looks like you were on the right track after all, but with some quotes misplaced.</p>

<pre><code>{% set entries = craft.entries.section('mySection') %}
{% set entry = entries.find({myFieldHandle:'mySearchVariable'})[0] %}
{{ entry.title }}
</code></pre>

<p>Although looks like this hits the database again (thanks carlcs).</p>

<p>Maybe back to the orginal answer then, which was:</p>

<pre><code>{% set entries = craft.entries.section('mySection') %}
{% for entry in entries if entry.myFieldHandle == 'myVariable' %}
    {{ entry.title }}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-04-17T14:33:09.487Z
I have a loop at present, which works. I was hoping there might be some kind of shortcut method that would filter the model by field value. Thanks though.
Russ Back
CC BY-SA 3.0
2015-04-17T17:08:24.107Z
After playing around some more I was able to get the 'find' working properly. I have updated the answer. See if that helps.
Douglas McDonald
CC BY-SA 3.0
2015-04-17T17:11:47.057Z
This adds another database query for each `find()`?
carlcs
CC BY-SA 3.0
2015-04-17T17:18:58.243Z
@carlcs hmm... ya... on second look I think you may be right.
Douglas McDonald
CC BY-SA 3.0
2015-04-17T03:38:06.150Z
2015-04-17T17:26:43.160Z
2015-04-17T17:26:43.160Z
Douglas McDonald
<p>In my experience the <code>search</code> function is a little more forgiving to work with than <code>find</code>.</p>

<p>See <a href="http://buildwithcraft.com/docs/searching" rel="nofollow">Searching</a> in the Craft documentation for the syntax and available search attributes.</p>

<pre><code>{% set entries = craft.entries.section('mySection') %}
{% set searchResults = entries.search('myFieldHandle:mySearchVariable') %}

{% if searchResults %}
  {% for entry in searchResults %}
    {{ entry.title }}
  {% endfor %}
{% endif %}
</code></pre>

<p>If you want to avoid the for loop and you're feeling lucky about the search result, you can always limit your search with <code>first()</code>.</p>

<pre><code>{% set entry = entries.search('myFieldHandle:mySearchVariable').first() %}

{% if entry %}
  {{ entry.title }}
{% endif %}
</code></pre>

<p>This does add extra queries for each search you run, tho.</p>

<p>If your goal is to avoid db queries, use a for loop to match against your search criteria. Optionally, you can use the <a href="https://github.com/marionnewlevant/craft-mnbreakandcontinue" rel="nofollow">MN Break and Continue</a> plugin to stop looping after the first match is made.</p>

<pre><code>{% for entry in entries if entry.myFieldHandle == 'myVariable' %}
  {{ entry.title }}
  {% break %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-04-20T12:24:24.393Z
2015-04-20T12:24:24.393Z
Alex Roper
<p>I have already retrieved a set of all Entries from a channel and need to now find one in the template without hitting the DB again (this find is in a loop so I don't want to grab a single result from the DB for every iteration through the loop):</p>

<pre><code>{% set entries = craft.entries.section('mySection').get %}
</code></pre>

<p>How can I then pluck out a specific entry by field match? I've tried this:</p>

<pre><code>{% set entry = entries.find('myFieldHandle', mySearchVariable) %}
</code></pre>

<p>This doesn't seem to work for me though in that I don't get a match.</p>

CC BY-SA 3.0
2015-04-17T00:38:01.530Z
Would it work to just search for the entries you are looking for in the first place instead of grabbing all the entries in the section?
Aaron Berkowitz
CC BY-SA 3.0
2015-04-17T14:33:53.637Z
I'm already doing that, however I was looking to avoid the loop in the template
Russ Back
CC BY-SA 3.0
2015-04-17T17:44:16.300Z
*"this find is in a loop"* – what do you mean with that?
carlcs
CC BY-SA 3.0
2015-04-17T17:45:39.750Z
Sorry ignore that part - there is no db overhead, I'm just looking to avoid looping in the template
Russ Back
<elementcriteriamodel>
How can I filter/find on an ElementCriteriaModel?
# How can I filter/find on an ElementCriteriaModel?

(Asked by: username_0 on 2015-04-16)

I have already retrieved a set of all Entries from a channel and need to now find one in the template without hitting the DB again (this find is in a loop so I don't want to grab a single result from the DB for every iteration through the loop):
{% set entries = craft.entries.section('mySection').get %}

How can I then pluck out a specific entry by field match? I've tried this:
{% set entry = entries.find('myFieldHandle', mySearchVariable) %}

This doesn't seem to work for me though in that I don't get a match.

**Tags:** elementcriteriamodel

> **Comments:**
> **username_1**: Would it work to just search for the entries you are looking for in the first place instead of grabbing all the entries in the section?  
> **username_0**: I'm already doing that, however I was looking to avoid the loop in the template  
> **username_2**: *"this find is in a loop"* – what do you mean with that?  
> **username_0**: Sorry ignore that part - there is no db overhead, I'm just looking to avoid looping in the template  

---

## 2 answers

### Answer by username_3 on 2015-04-17. Score: 2

Update
I may have found a better option for you. Looks like you were on the right track after all, but with some quotes misplaced.
{% set entries = craft.entries.section('mySection') %}
{% set entry = entries.find({myFieldHandle:'mySearchVariable'})[0] %}
{{ entry.title }}

Although looks like this hits the database again (thanks username_2).
Maybe back to the orginal answer then, which was:
{% set entries = craft.entries.section('mySection') %}
{% for entry in entries if entry.myFieldHandle == 'myVariable' %}
    {{ entry.title }}
{% endfor %}

> **Comments:**
> **username_0**: I have a loop at present, which works. I was hoping there might be some kind of shortcut method that would filter the model by field value. Thanks though.  
> **username_3**: After playing around some more I was able to get the 'find' working properly. I have updated the answer. See if that helps.  
> **username_2**: This adds another database query for each `find()`?  
> **username_3**: @username_2 hmm... ya... on second look I think you may be right.  

---

### Answer by username_4 on 2015-04-20. Score: 0

In my experience the search function is a little more forgiving to work with than find.
See Searching in the Craft documentation for the syntax and available search attributes.
{% set entries = craft.entries.section('mySection') %}
{% set searchResults = entries.search('myFieldHandle:mySearchVariable') %}

{% if searchResults %}
  {% for entry in searchResults %}
    {{ entry.title }}
  {% endfor %}
{% endif %}

If you want to avoid the for loop and you're feeling lucky about the search result, you can always limit your search with first().
{% set entry = entries.search('myFieldHandle:mySearchVariable').first() %}

{% if entry %}
  {{ entry.title }}
{% endif %}

This does add extra queries for each search you run, tho.
If your goal is to avoid db queries, use a for loop to match against your search criteria. Optionally, you can use the MN Break and Continue plugin to stop looping after the first match is made.
{% for entry in entries if entry.myFieldHandle == 'myVariable' %}
  {{ entry.title }}
  {% break %}
{% endfor %}


---

