<p>You can loop though your "myMatrixBlockName" blocks to get all the retailers' entry IDs. Then use that array of IDs in a <code>craft.entries</code> ElementCriteriaModel to get the entries you're after. Use <code>fixedOrder</code> as a parameter to maintain the Matrix's sort order if you want that.</p>

<pre><code>{% set retailerIds = [] %}

{# Loop `myMatrixBlockName` blocks to get the retailers' entry IDs #}
{% for block in entry.mensPDPLinks.type('myMatrixBlockName') %}

    {% set retailerId = block.retailer %}
    {% set retailerIds = retailerIds|merge([retailerId]) %}

{% endfor %}

{# Convert `retailerIds` array into comma separated list #}
{% set retailerIdString = retailerIds|join(', ') %}

{# Get `retailers` in the same order as listed in the Matrix field #}
{% set retailers = craft.entries.id(retailerIdString).fixedOrder %}

{# Loop `retailers` #}
{% for retailer in retailers %}

    {{ retailer.title }}

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

CC BY-SA 3.0
2014-10-29T16:55:31.610Z
Great, thanks. I had to change it slightly to make it work. Namely, converting the array to a comma-separated string. After the `for` loop I did:

    `{% set retailerIdString = retailerIds | join(', ') %}`

Then passed that to `craft.entries`.

Thanks for the help.
Brett DeWoody
CC BY-SA 3.0
2014-10-29T16:58:23.287Z
Yep ok, didn't test the code at all. Good that it works for you now, will update the code. Thanks for the feedback, @Brett!
carlcs
CC BY-SA 3.0
2014-10-29T15:52:05.987Z
2014-10-29T17:00:32.263Z
2014-10-29T17:00:32.263Z
carlcs
<p>I have a Matrix field where one of my child fields is a Dynamic Dropdown that uses the following code to pull in entries:</p>

<pre><code>{% for entry in craft.entries.limit(null).section('dealers').type('online') %}
  { "value":"{{ entry.id }}" , "label":"{{ entry | raw }}"
    {% if entry.uri == '__home__' %} , "default":true{% endif %}} 
    {% if not loop.last %},{% endif %}
{% endfor %}
</code></pre>

<p>The Matrix field has a handle of <code>mensPDPLinks</code> and the child field has a handle of <code>retailer</code>. </p>

<p>The above code creates a dropdown containing a list of retailers from another section.</p>

<p>When editing an entry containing this field a person can add as many retailers as needed by adding additional Matrix rows. It should be storing the entryID for each of the retailer entries they select. There are additional Matrix child fields to include other data to each retailer they select. </p>

<p>In my templates I'd like to loop through the entries (retailers) they've added in the Matrix field and display some data from the retailers entry. </p>

<p>Can someone point me in a direction? I'm kinda lost as to how I can do this. </p>

<matrix><relations>
Related entries from a dynamic dropdown
# Related entries from a dynamic dropdown

(Asked by: username_0 on 2014-10-29)

I have a Matrix field where one of my child fields is a Dynamic Dropdown that uses the following code to pull in entries:
{% for entry in craft.entries.limit(null).section('dealers').type('online') %}
  { "value":"{{ entry.id }}" , "label":"{{ entry | raw }}"
    {% if entry.uri == '__home__' %} , "default":true{% endif %}} 
    {% if not loop.last %},{% endif %}
{% endfor %}

The Matrix field has a handle of mensPDPLinks and the child field has a handle of retailer. 
The above code creates a dropdown containing a list of retailers from another section.
When editing an entry containing this field a person can add as many retailers as needed by adding additional Matrix rows. It should be storing the entryID for each of the retailer entries they select. There are additional Matrix child fields to include other data to each retailer they select. 
In my templates I'd like to loop through the entries (retailers) they've added in the Matrix field and display some data from the retailers entry. 
Can someone point me in a direction? I'm kinda lost as to how I can do this.

**Tags:** matrix, relations


---

## 1 answers

### Answer by username_1 on 2014-10-29. Score: 2 | Accepted answer

You can loop though your "myMatrixBlockName" blocks to get all the retailers' entry IDs. Then use that array of IDs in a craft.entries ElementCriteriaModel to get the entries you're after. Use fixedOrder as a parameter to maintain the Matrix's sort order if you want that.
{% set retailerIds = [] %}

{# Loop `myMatrixBlockName` blocks to get the retailers' entry IDs #}
{% for block in entry.mensPDPLinks.type('myMatrixBlockName') %}

    {% set retailerId = block.retailer %}
    {% set retailerIds = retailerIds|merge([retailerId]) %}

{% endfor %}

{# Convert `retailerIds` array into comma separated list #}
{% set retailerIdString = retailerIds|join(', ') %}

{# Get `retailers` in the same order as listed in the Matrix field #}
{% set retailers = craft.entries.id(retailerIdString).fixedOrder %}

{# Loop `retailers` #}
{% for retailer in retailers %}

    {{ retailer.title }}

{% endfor %}

> **Comments:**
> **username_0**: Great, thanks. I had to change it slightly to make it work. Namely, converting the array to a comma-separated string. After the `for` loop I did:      `{% set retailerIdString = retailerIds | join(', ') %}`  Then passed that to `craft.entries`.  Thanks for the help.  
> **username_1**: Yep ok, didn't test the code at all. Good that it works for you now, will update the code. Thanks for the feedback, @Brett!  

---

