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

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

CC BY-SA 3.0
2015-03-03T00:18:30.833Z
I added the actual field in the block type to the example. But, trying what you said doesn't work unfortunately.
John
CC BY-SA 3.0
2015-03-03T00:23:38.887Z
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`?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-03-03T00:34:18.213Z
Hmm, yes you are right, using contentBuilder.content works along with targetElement. Superb! Thanks Mats!!
John
CC BY-SA 3.0
2015-03-03T00:44:11.727Z
Awesome! I edited the answer to include that detail, for reference.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-03-02T23:48:18.637Z
2015-03-03T00:43:08.657Z
2015-03-03T00:43:08.657Z
Mats Mikkel Rummelhoff
<p>I've got a Section that uses a Matrix field, that has a block, that has a categories field in it.</p>

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

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

<pre><code>{% 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 %}
</code></pre>

<matrix><categories><relations>
Find entries that have a category selected from a category field within a matrix block
# Find entries that have a category selected from a category field within a matrix block

(Asked by: username_0 on 2015-03-02)

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 %}

**Tags:** matrix, categories, relations


---

## 1 answers

### Answer by username_1 on 2015-03-02. Score: 3 | Accepted answer

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`.

> **Comments:**
> **username_0**: I added the actual field in the block type to the example. But, trying what you said doesn't work unfortunately.  
> **username_1**: 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`?  
> **username_0**: Hmm, yes you are right, using contentBuilder.content works along with targetElement. Superb! Thanks Mats!!  
> **username_1**: Awesome! I edited the answer to include that detail, for reference.  

---

