<p>You can access the assets field by referencing the matrix field first. <code>myMatrixFieldHandle.assets</code>.  So your entries code would be something like this.</p>

<pre><code>{% set entries = craft.entries.section('menu').limit(3).order('RAND()').relatedTo({
       targetElement: assets,
       field: 'myMatrixFieldHandle.assets'
}) %}
</code></pre>

CC BY-SA 3.0
2015-10-30T01:41:14.713Z
Thanks but I've got this error instead. Craft\EntryModel and its behaviors do not have a method or closure named "assets".
Mark H.
CC BY-SA 3.0
2015-10-30T02:32:23.857Z
It would be whatever you named your assets field.
a-am
CC BY-SA 3.0
2015-10-30T19:40:35.887Z
In my case the matrix fieldtype called "contentBuilder" and assets field name is "image.

    {% set assets = craft.assets.limit(null) %}
    {% set entries = craft.entries.section('menu').limit(3).order('RAND()').relatedTo({
       targetElement: assets,
       field: 'contentBuilder.image'
}) %}

    {% for item in entries %}
    {% set i = item.image.first() %}
    <img src="{{ i.getUrl('medium') }}" />
    {% endfor %}
Mark H.
CC BY-SA 3.0
2015-10-29T03:39:10.980Z
2015-10-29T03:39:10.980Z
a-am
<p>I was using this code in the past and it worked well</p>

<pre><code>        {% set assets = craft.assets.limit(null) %}
        {% set entries = craft.entries.section('menu').limit(3).order('RAND()').relatedTo({
            targetElement: assets,
            field: 'assets'
        }) %}

        {% for item in entries %}
                {% set i = item.assets.first() %}

                &lt;img width="{{ i.getWidth('medium') }}" height="{{ i.getHeight('medium') }}" src="{{ i.getUrl('medium') }}" alt="{{ recipe.title }}" /&gt;

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

<p>However, in the new version the standalone "assets" field became a part of the matrix field so this piece of code doesn't work anymore. Please advise :)</p>

<matrix>
Sort by Matrix Field
# Sort by Matrix Field

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

I was using this code in the past and it worked well
        {% set assets = craft.assets.limit(null) %}
        {% set entries = craft.entries.section('menu').limit(3).order('RAND()').relatedTo({
            targetElement: assets,
            field: 'assets'
        }) %}

        {% for item in entries %}
                {% set i = item.assets.first() %}

                

        {% endfor %}

However, in the new version the standalone "assets" field became a part of the matrix field so this piece of code doesn't work anymore. Please advise :)

**Tags:** matrix


---

## 1 answers

### Answer by username_1 on 2015-10-29. Score: 1

You can access the assets field by referencing the matrix field first. myMatrixFieldHandle.assets.  So your entries code would be something like this.
{% set entries = craft.entries.section('menu').limit(3).order('RAND()').relatedTo({
       targetElement: assets,
       field: 'myMatrixFieldHandle.assets'
}) %}

> **Comments:**
> **username_0**: Thanks but I've got this error instead. Craft\EntryModel and its behaviors do not have a method or closure named "assets".  
> **username_1**: It would be whatever you named your assets field.  
> **username_0**: In my case the matrix fieldtype called "contentBuilder" and assets field name is "image.      {% set assets = craft.assets.limit(null) %}     {% set entries = craft.entries.section('menu').limit(3).order('RAND()').relatedTo({        targetElement: assets,        field: 'contentBuilder.image' }) %}      {% for item in entries %}     {% set i = item.image.first() %}     <img src="{{ i.getUrl('medium') }}" />     {% endfor %}  

---

