<p>I think your first idea of using Matrix rather than Categories makes the most sense as a content model.</p>

<p>I recently added a new method to the plugin <a href="https://github.com/low/lowblocks" rel="nofollow">lowblocks</a> which should help you. Low has already merged it into the core so go and install it now.</p>

<p>This should work for you hopefully:</p>

<pre><code>{# loop through the instruments #}
{% for instrument in craft.entries.section('instrument') %}

    &lt;h3&gt;{{ instrument.instrumentName }}&lt;/h3&gt;

    {# select all our musician blocks related to this instrument #}
    {% set musicians = craft.lowblocks.blocks.type('musiciansBlockHandle').relatedTo(instrument) %}

    {% if musicians | length %}

        {# loop through the results #}
        {% for musician in musicians %}

            {# set a variable to store the person (the entry/owner of this block #}
            {% set person = musician.owner %}

            &lt;p&gt;{{ person.personName }}&lt;/p&gt;

        {% endfor %}

    {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-09-16T16:51:56.053Z
Went ahead and gave it a shot. Works pretty well. Thanks for the tip. Does this mean Craft does not have a native way to query by Matrix field block type in templates?
Bill Columbia
CC BY-SA 3.0
2014-09-13T06:10:44.100Z
2014-09-13T06:10:44.100Z
Jamie Pittock
<p>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 <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="nofollow">relatedTo</a> parameter.</p>

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

<p>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.</p>

CC BY-SA 3.0
2014-11-03T20:49:12.263Z
Awesome thanks for this tip!
Bill Columbia
CC BY-SA 3.0
2014-10-30T22:49:23.893Z
2014-10-30T22:49:23.893Z
Kevin Ouellette
<p>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. </p>

<p>This allows someone to be a musician and to be assigned an instrument.</p>

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

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

<p>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:</p>

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

CC BY-SA 3.0
2014-09-13T00:53:50.247Z
I should probably elaborate that this code does not work.
Bill Columbia
<matrix><categories><relations>
How to query with relatedTo of a Relationship Field in a Matrix Field block
# How to query with relatedTo of a Relationship Field in a Matrix Field block

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

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') %}
          {{ instrument.instrumentName }}
          {% for person in craft.entries.section('person').relatedTo( instrument ) %}
            {{ person.personName }}
          {% endfor %}
        {% endfor %}

**Tags:** matrix, categories, relations

> **Comments:**
> **username_0**: I should probably elaborate that this code does not work.  

---

## 2 answers

### Answer by username_1 on 2014-09-13. Score: 2 | Accepted answer

I think your first idea of using Matrix rather than Categories makes the most sense as a content model.
I recently added a new method to the plugin lowblocks which should help you. Low has already merged it into the core so go and install it now.
This should work for you hopefully:
{# loop through the instruments #}
{% for instrument in craft.entries.section('instrument') %}

    {{ instrument.instrumentName }}

    {# select all our musician blocks related to this instrument #}
    {% set musicians = craft.lowblocks.blocks.type('musiciansBlockHandle').relatedTo(instrument) %}

    {% if musicians | length %}

        {# loop through the results #}
        {% for musician in musicians %}

            {# set a variable to store the person (the entry/owner of this block #}
            {% set person = musician.owner %}

            {{ person.personName }}

        {% endfor %}

    {% endif %}

{% endfor %}

> **Comments:**
> **username_0**: Went ahead and gave it a shot. Works pretty well. Thanks for the tip. Does this mean Craft does not have a native way to query by Matrix field block type in templates?  

---

### Answer by username_2 on 2014-10-30. Score: 3

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') %}
      {{ instrument.instrumentName }}
      {% set people = craft.entries.section('person').relatedTo({
         targetElement: instrument,
         field: 'roles.instrument'
      }) %}
      {% if people|length %}
         {% for person in people %}
           {{ person.personName }}
         {% endfor %}
      {% else %}
         Nobody plays {{ instrument.instrumentName }} :(
      {% 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.

> **Comments:**
> **username_0**: Awesome thanks for this tip!  

---

