I'm not entirely sure how to search a specific Matrix field for a given bit of content. I can use:

{% for cta in entry.homeCtas.search('"Lorem Ipsum"') %}
    <h1>{{ cta.ctaTitle }}</h1>
{% endfor %}

to find any Matrix rows with "Lorem Ipsum" (not in a specific field). But when I try something like this:

{% for cta in entry.homeCtas.search('ctaTitle:"Lorem Ipsum"') %}
    <h1>{{ cta.ctaTitle }}</h1>
{% endfor %}

It returns nothing. Is my field syntax incorrect? Am I missing something obvious?

  • I think this could help: craftcms.stackexchange.com/a/2104/125 – carlcs Aug 24 '14 at 22:12
  • Hm, I'm not certain that confirms/denies an ability to search a specific field within a Matrix, though. The comment at the end ("You can't use search('fileId:sitelogo'), because that would filter for entries with fileId:sitelogo and not entries with a matrix block fileId:sitelogo.") makes it sound like this user is explaining why the author's original code doesn't work. I don't see anything about if it's possible or not. – Aaron Bushnell Aug 24 '14 at 22:26
  • There is no such thing as a matrixcriteriamodel. Only type and limit filter is possible (at least that's the only filters documented). – carlcs Aug 24 '14 at 22:33
  • This didn't help, Aaron? – carlcs Aug 25 '14 at 7:31
  • Sorry, carlcs. Thanks for clarifying that for me! Hopefully one day Matrix will have this level of support. – Aaron Bushnell Aug 25 '14 at 17:30

What I'm not clear of is if you are asking if you can query for entries that have a matrix field that contains x, or if you just asking if you can search within a matrix field of an entry that has already been returned by a query.

If it is the later, the following solution would work. This isn't a feature of Craft, but a feature of Twig. Thus, implementing it won't save on queries or anything, but it will help you filter the results returned by Craft.

Twig has a feature that allows you to apply a condition in addition to the for loop. It is documented here.

{% for cta in entry.homeCtas if cta.ctaTitle == "Lorem Ipsum" %}
    <h1>{{ cta.ctaTitle }}</h1>
{% endfor %}
  • Thanks, Wes. I should have clarified this further. I was working with the latter situation. I'll give this a shot! – Aaron Bushnell Aug 25 '14 at 2:11

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.