1

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.

  • 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 Apr 17 '15 at 0:38
  • I'm already doing that, however I was looking to avoid the loop in the template – Russ Back Apr 17 '15 at 14:33
  • "this find is in a loop" – what do you mean with that? – carlcs Apr 17 '15 at 17:44
  • Sorry ignore that part - there is no db overhead, I'm just looking to avoid looping in the template – Russ Back Apr 17 '15 at 17:45
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 carlcs).

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 %}
  • 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 Apr 17 '15 at 14:33
  • 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 Apr 17 '15 at 17:08
  • This adds another database query for each find()? – carlcs Apr 17 '15 at 17:11
  • @carlcs hmm... ya... on second look I think you may be right. – Douglas McDonald Apr 17 '15 at 17:18
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 %}

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.