<p>You probably have to query a new ElementCriteriaModel for each asset in your loop and use the <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="noreferrer"><code>relatedTo</code></a> parameter to filter the relevant "content" entries.</p>

<p>You could use the <code>targetElement</code> property with the <code>relatedTo</code> parameter to be more spezific on that relation. But you should be fine using the <code>element</code> property or the short syntax <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="noreferrer"><code>relatedTo(element)</code></a>, if you don't do any (reverse) relations (e.g. with an entries field as an <a href="http://buildwithcraft.com/docs/assets#asset-meta-fields" rel="noreferrer">asset meta field</a>).</p>

<p>Tested this on an install with 400 images and yes, you want to wrap it in <code>cache</code> tags!</p>

<pre><code>{% set content = craft.entries.section('content').find() %}
{% set usedFotos = craft.assets.relatedTo(content).order('filename').find() %}

{# Loop throught the array of assets #}
{% for usedFoto in usedFotos %}

    &lt;h2&gt;{{ usedFoto.filename }}&lt;/h2&gt;
    &lt;ul&gt;

        {# Loop throught entries related to this asset #}
        {% for entry in craft.entries.relatedTo(usedFoto).order('title') %}
            &lt;li&gt;{{ entry.title }}&lt;/li&gt;
        {% endfor %}

    &lt;/ul&gt;

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

CC BY-SA 3.0
2014-08-19T11:23:30.667Z
2014-08-19T11:32:23.077Z
2014-08-19T11:32:23.077Z
carlcs
<p>Given a list of assets, for each asset, how do I display a list of entries that relate to this asset? So, for instance, if asset A is selected as asset in entries 1 and 2, I want a list to be displayed like this:</p>

<ul>
<li>Asset A:
<ul>
<li>Used in entry 1</li>
<li>Used in entry 2</li>
</ul></li>
<li>Asset B:
<ul>
<li>Used in ... etc.</li>
</ul></li>
</ul>

<p>With this ...</p>

<pre><code>{% set content = craft.entries.section('content').find() %}
{% set usedFotos = craft.assets.relatedTo(content).find() | group('id') %}
</code></pre>

<p>... I can see if certain assets are used, but now I want to see what entr(y/ies) they belong to.</p>

CC BY-SA 3.0
2014-08-21T16:13:24.347Z
Paul, did the answer I provided help you with your problem?
carlcs
CC BY-SA 3.0
2014-08-21T16:16:18.327Z
Didn't have a chance to try yet, will do tonight. Was hoping for a 'one-query solution' ;)
Paul
CC BY-SA 3.0
2014-08-21T16:21:48.673Z
Yeah I also hoped to find a solution with less DB queries, but I don't think it's possible.
carlcs
<entries><assets><relations><assetfilemodel>
How do I display a list of assets with the entries they are related to?
# How do I display a list of assets with the entries they are related to?

(Asked by: username_0 on 2014-08-19)

Given a list of assets, for each asset, how do I display a list of entries that relate to this asset? So, for instance, if asset A is selected as asset in entries 1 and 2, I want a list to be displayed like this:

Asset A:

Used in entry 1
Used in entry 2

Asset B:

Used in ... etc.


With this ...
{% set content = craft.entries.section('content').find() %}
{% set usedFotos = craft.assets.relatedTo(content).find() | group('id') %}

... I can see if certain assets are used, but now I want to see what entr(y/ies) they belong to.

**Tags:** entries, assets, relations, assetfilemodel
> **Comments:**
> **username_1**: Paul, did the answer I provided help you with your problem?  
> **username_0**: Didn't have a chance to try yet, will do tonight. Was hoping for a 'one-query solution' ;)  
> **username_1**: Yeah I also hoped to find a solution with less DB queries, but I don't think it's possible.  

---

## 1 answers

### Answer by username_1 on 2014-08-19. Score: 5 | Accepted answer

You probably have to query a new ElementCriteriaModel for each asset in your loop and use the relatedTo parameter to filter the relevant "content" entries.
You could use the targetElement property with the relatedTo parameter to be more spezific on that relation. But you should be fine using the element property or the short syntax relatedTo(element), if you don't do any (reverse) relations (e.g. with an entries field as an asset meta field).
Tested this on an install with 400 images and yes, you want to wrap it in cache tags!
{% set content = craft.entries.section('content').find() %}
{% set usedFotos = craft.assets.relatedTo(content).order('filename').find() %}

{# Loop throught the array of assets #}
{% for usedFoto in usedFotos %}

    {{ usedFoto.filename }}
    

        {# Loop throught entries related to this asset #}
        {% for entry in craft.entries.relatedTo(usedFoto).order('title') %}
            {{ entry.title }}
        {% endfor %}

    

{% endfor %}


---

