<p>To access related entries stored in a matrix field, you need to specify the <code>targetElement</code> (actor/entry) and the <code>matrix field</code> (using dot syntax 'matrixFieldHandle.entriesFieldHandle') from 'plays'. </p>

<p>In your actor biography template:</p>

<pre><code>Actor: {{ entry.title }}&lt;br&gt;&lt;br&gt;

{% set plays = craft.entries.section('plays').relatedTo({
    targetElement: entry,
    field: 'actorsMatrixFieldHandle.actor'
}) %}

Plays:
&lt;ul&gt;
    {% for play in plays %}
        &lt;li&gt;
            {{ play.title }}&lt;br&gt;
            {% if play.onStage %}
                &lt;span class="onstage"&gt;ON STAGE&lt;/span&gt;
            {% endif %}
        &lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>To get the role is little more difficult — you first need to find the actor's <code>matrix block</code> in the related 'play' record. We can do this by testing each block to see if it matches the current <code>entry</code> (actor), or use an <code>if</code> clause in the loop.</p>

<pre><code>&lt;ul&gt;
    {% for play in plays %}

        {% set role = '' %}
        {% for block in play.actorsMatrixFieldHandle if block.actor.first.title == entry.title %}
            {% set role = block.role %}
        {% endfor %}

        &lt;li&gt;
            {{ play.title }}&lt;br&gt; 
            {{ role }}&lt;br&gt;
            {% if play.onStage %}
                &lt;span class="onstage"&gt;ON STAGE&lt;/span&gt;
            {% endif %}
        &lt;/li&gt;

    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>You could also try to set the <code>title</code> field as a parameter to the Matrix block query:</p>

<pre><code>{% set actorBlocks = play.actorsMatrixFieldHandle.title(entry.title) %}
{% set role = actorBlocks|length ? actorBlocks.first().role : '' %}
</code></pre>

CC BY-SA 3.0
2015-04-19T08:48:01.600Z
It might be possible to add a `myCustomField` parameter to the Matrix block query to get rid of the conditional / extra loop.
carlcs
CC BY-SA 3.0
2015-04-19T15:07:37.153Z
@carlcs Sounds great - but not sure what your suggesting exactly. (Feel free to add another code snippet to the answer.)
Douglas McDonald
CC BY-SA 3.0
2015-04-19T18:04:12.320Z
Douglas, Craft 2.3 introduced setting custom fields as a param to Matrix block queries, so it's not only possible to filter by block type but any other block property (→ [2.3 release notes](http://buildwithcraft.com/updates#build2615) under templating).
carlcs
CC BY-SA 3.0
2015-04-19T18:54:03.247Z
Didn't know that — much better solution! Thanks.
Douglas McDonald
CC BY-SA 3.0
2015-04-18T23:49:48.860Z
2015-04-19T19:20:33.480Z
2015-04-19T19:20:33.480Z
Douglas McDonald
<p>On our play pages (ex. <a href="http://actors-express.com.s102768.gridserver.com/plays/liaisons" rel="nofollow noreferrer">http://actors-express.com.s102768.gridserver.com/plays/liaisons</a>), we list our each of the cast members, their headshot and role. We did this by creating a matrix field that pulls in the Entry for each person and relates it to the specifics for the show.
<img src="https://i.sstatic.net/iGcS2.png" alt="List of Cast Members, Headshot and Role"></p>

<p>We would like to write code on the template for their bio page (ex. <a href="http://actors-express.com.s102768.gridserver.com/people/kristin-butler" rel="nofollow noreferrer">http://actors-express.com.s102768.gridserver.com/people/kristin-butler</a>) that will scan through all those matrices on play pages and find out which plays they have been in, then display a list of plays with links to each of their URLs. It would be nice to pull in what their role was in the play, but that's not a requirement at this point.</p>

<p><img src="https://i.sstatic.net/qS9ZE.png" alt="List of Past Plays and Widget for Upcoming Play"></p>

<p>We would also perform a lookup and see if one of the plays was currently on stage (a flag on our play entry type), or if it was upcoming to pull it into the module above on the right.</p>

<matrix><entry><entry-type><entrymodel>
Pulling Data Across Entry Types
# Pulling Data Across Entry Types

(Asked by: username_0 on 2015-04-18)

On our play pages (ex. <http://actors-express.com.s102768.gridserver.com/plays/liaisons>), we list our each of the cast members, their headshot and role. We did this by creating a matrix field that pulls in the Entry for each person and relates it to the specifics for the show.
![List of Cast Members, Headshot and Role](https://i.sstatic.net/iGcS2.png)

We would like to write code on the template for their bio page (ex. <http://actors-express.com.s102768.gridserver.com/people/kristin-butler>) that will scan through all those matrices on play pages and find out which plays they have been in, then display a list of plays with links to each of their URLs. It would be nice to pull in what their role was in the play, but that's not a requirement at this point.

![List of Past Plays and Widget for Upcoming Play](https://i.sstatic.net/qS9ZE.png)

We would also perform a lookup and see if one of the plays was currently on stage (a flag on our play entry type), or if it was upcoming to pull it into the module above on the right.

**Tags:** matrix, entry, entry-type, entrymodel

---

## 1 answers

### Answer by username_1 on 2015-04-18. Score: 4 | Accepted answer

To access related entries stored in a matrix field, you need to specify the targetElement (actor/entry) and the matrix field (using dot syntax 'matrixFieldHandle.entriesFieldHandle') from 'plays'. 
In your actor biography template:
Actor: {{ entry.title }}

{% set plays = craft.entries.section('plays').relatedTo({
    targetElement: entry,
    field: 'actorsMatrixFieldHandle.actor'
}) %}

Plays:

    {% for play in plays %}
        
            {{ play.title }}
            {% if play.onStage %}
                ON STAGE
            {% endif %}
        
    {% endfor %}


To get the role is little more difficult — you first need to find the actor's matrix block in the related 'play' record. We can do this by testing each block to see if it matches the current entry (actor), or use an if clause in the loop.

    {% for play in plays %}

        {% set role = '' %}
        {% for block in play.actorsMatrixFieldHandle if block.actor.first.title == entry.title %}
            {% set role = block.role %}
        {% endfor %}

        
            {{ play.title }} 
            {{ role }}
            {% if play.onStage %}
                ON STAGE
            {% endif %}
        

    {% endfor %}


You could also try to set the title field as a parameter to the Matrix block query:
{% set actorBlocks = play.actorsMatrixFieldHandle.title(entry.title) %}
{% set role = actorBlocks|length ? actorBlocks.first().role : '' %}

> **Comments:**
> **username_2**: It might be possible to add a `myCustomField` parameter to the Matrix block query to get rid of the conditional / extra loop.  
> **username_1**: @carlcs Sounds great - but not sure what your suggesting exactly. (Feel free to add another code snippet to the answer.)  
> **username_2**: Douglas, Craft 2.3 introduced setting custom fields as a param to Matrix block queries, so it's not only possible to filter by block type but any other block property (→ [2.3 release notes](http://buildwithcraft.com/updates#build2615) under templating).  
> **username_1**: Didn't know that — much better solution! Thanks.  

---

