1

I've got a Section that uses a Matrix field, that has a block, that has a categories field in it.

I want to find all entries using this Matrix field, who has a categories field and has a certain category selected.

I'm looking at the Relations documentation and trying the following code, but perhaps i'm barking up the wrong tree here?

{% set cat = craft.categories.slug('print') %}
{% set entries = craft.entries.section('projectsEntries').relatedTo({
sourceElement: cat,
field: 'contentBuilder.relatedCategories.content'
}) %}

{% for entry in entries %}

{{ entry.title }}

{% endfor %}

1 Answer 1

3

Try using targetElement: cat instead of sourceElement – in my experience the entries are considered the source of the relation in cases like these, not the categories.

Edit: Also, the field parameter of the relatedTo method shouldn't include the Matrix field's block type, only the actual category field – i.e. contentBuilder.content, not contentBuilder.relatedCategories.content.

4
  • I added the actual field in the block type to the example. But, trying what you said doesn't work unfortunately.
    – John
    Mar 3, 2015 at 0:18
  • Hm. There's no need to include/specify the block type in the field parameter, contentBuilder.content should do nicely if the latter is your actual category field. Did you try with both targetElement and contentBuilder.content? Mar 3, 2015 at 0:23
  • Hmm, yes you are right, using contentBuilder.content works along with targetElement. Superb! Thanks Mats!!
    – John
    Mar 3, 2015 at 0:34
  • Awesome! I edited the answer to include that detail, for reference. Mar 3, 2015 at 0:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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