Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-30T01:41:14.713Z
Answers.Text: Thanks but I've got this error instead. Craft\EntryModel and its behaviors do not have a method or closure named "assets".
Answers.UserDisplayName: Mark H.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-30T02:32:23.857Z
Answers.Text: It would be whatever you named your assets field.
Answers.UserDisplayName: a-am
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-30T19:40:35.887Z
Answers.Text: 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 %}
Answers.UserDisplayName: Mark H.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-29T03:39:10.980Z
Answers.LastActivityDate: 2015-10-29T03:39:10.980Z
Answers.UserDisplayName: a-am
Body: <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>
  
Tags: <matrix>
Title: Sort by Matrix Field
ThreadText: # 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() %}
  
                  <img width="{{ i.getWidth('medium') }}" height="{{ i.getHeight('medium') }}" src="{{ i.getUrl('medium') }}" alt="{{ recipe.title }}" />
  
          {% 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 %}  
  
  ---
  
  