How to query with relatedTo of a Relationship Field in a Matrix Field block I have structures called Instrument and Person. On the person structure, I have a Matrix field called Roles. Roles has a block called Musician. And That block type has a Relationship Field called Instrument. 

This allows someone to be a musician and to be assigned an instrument.

In my template I need to output each Instrument as a section, and all of the musicians that play that instrument.

I think this issue has something to do with the relationship being added in a Matrix block rather than on the entry itself. 

Unfortunately there are many roles (block types) that a person can have, each with different fields. I tried to solve this by swapping to Instruments as categories but that doesn't seem to be working either. Here's some sample code I've been playing with:

        {% for instrument in craft.entries.section('instrument') %}
          &lt;h3&gt;{{ instrument.instrumentName }}&lt;/h3&gt;
          {% for person in craft.entries.section('person').relatedTo( instrument ) %}
            &lt;p&gt;{{ person.personName }}&lt;/p&gt;
          {% endfor %}
        {% endfor %}
You were right about the issue being that the relationship is made within a Matrix field. Because of this, you have to explicitly pass the Matrix field's handle to the relatedTo parameter.

{% for instrument in craft.entries.section('instrument') %}
      &lt;h3&gt;{{ instrument.instrumentName }}&lt;/h3&gt;
      {% set people = craft.entries.section('person').relatedTo({
         targetElement: instrument,
         field: 'roles.instrument'
      }) %}
      {% if people|length %}
         {% for person in people %}
           &lt;p&gt;{{ person.personName }}&lt;/p&gt;
         {% endfor %}
      {% else %}
         &lt;p&gt;Nobody plays {{ instrument.instrumentName }} :(&lt;/p&gt;
      {% endif %}
{% endfor %}


Note: Using a dot notation to specify the sub-field is only necessary if you have multiple relational fields within a Matrix, but it can't hurt.