<p>When you relate an element from a Matrix block, it's important to know that the relation is between the <em>Matrix block</em> and the related element; nor the <em>entry</em> (or whatever has the Matrix field) and the related element.</p>

<p>So, the correct way to select related elements through a Matrix field is:</p>

<pre><code>{% set employer = craft.entries.section('companies').slug(segment) %}
{% set personnel = craft.entries.section('people').relatedTo({
    targetElement: employer,
    field: "matrixField.relationField"
}) %}
</code></pre>

<p>Update: Added a missing } on the last line.</p>

CC BY-SA 3.0
2014-10-10T03:37:41.687Z
Thanks Brandon.  Just getting up to speed with Craft.  You guys are amazing!
V. Arora
CC BY-SA 3.0
2014-09-02T05:15:13.760Z
2014-10-14T14:08:51.897Z
2014-10-14T14:08:51.897Z
Brandon Kelly
<p>Consider an employee listing...</p>

<p>I have 2 sections: 1 for companies, 1 for personnel.  In the personnel section, there is a matrix field within which one field is a relational one to relate the staff member to his company.</p>

<p>On the front end, I'm attempting to display a listing of all staff associated with a particular company.</p>

<p>I can't seem to get the relational fields to work in the way that I want. I know I'm missing something basic here.</p>

<pre><code>{% set employer = craft.entries.section('companies').slug(segment) %}
{% set personnel = craft.entries.section('people').relatedTo(employer) %}

{% for x in personnel %}
    {{ x.title }}
{% endfor %}

// Output: nothing
</code></pre>

<p>Is there something particular to Matrix fields that changes how I might do this?  I've tried using the targetElement parameter but still can't seem to get it to work.</p>

<p>Thanks for the help!</p>

<matrix><relations>
How can I display related entries selected within a Matrix field?
# How can I display related entries selected within a Matrix field?

(Asked by: username_0 on 2014-09-02)

Consider an employee listing...
I have 2 sections: 1 for companies, 1 for personnel.  In the personnel section, there is a matrix field within which one field is a relational one to relate the staff member to his company.
On the front end, I'm attempting to display a listing of all staff associated with a particular company.
I can't seem to get the relational fields to work in the way that I want. I know I'm missing something basic here.
{% set employer = craft.entries.section('companies').slug(segment) %}
{% set personnel = craft.entries.section('people').relatedTo(employer) %}

{% for x in personnel %}
    {{ x.title }}
{% endfor %}

// Output: nothing

Is there something particular to Matrix fields that changes how I might do this?  I've tried using the targetElement parameter but still can't seem to get it to work.
Thanks for the help!

**Tags:** matrix, relations

---

## 1 answers

### Answer by username_1 on 2014-09-02. Score: 7 | Accepted answer

When you relate an element from a Matrix block, it's important to know that the relation is between the Matrix block and the related element; nor the entry (or whatever has the Matrix field) and the related element.
So, the correct way to select related elements through a Matrix field is:
{% set employer = craft.entries.section('companies').slug(segment) %}
{% set personnel = craft.entries.section('people').relatedTo({
    targetElement: employer,
    field: "matrixField.relationField"
}) %}

Update: Added a missing } on the last line.

> **Comments:**
> **username_0**: Thanks Brandon.  Just getting up to speed with Craft.  You guys are amazing!  

---

