Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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?

share|improve this question
    
I think this could help: craftcms.stackexchange.com/a/2104/125 –  carlcs Aug 24 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 at 22:26
    
This user is me ;) and I'm not totally sure but as far as I know it's not possible. I think you can only search for entries where any field contains that string. –  carlcs Aug 24 at 22:30
    
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 at 22:33
    
This didn't help, Aaron? –  carlcs Aug 25 at 7:31

1 Answer 1

up vote 1 down vote accepted

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 %}
share|improve this answer
    
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 at 2:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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