<p>You're close! From looking at your code, it seems like you're eventually querying for the related assets themselves though – not the entries those assets are related to. So essentially, you'll need to work backwards vs. what you're currently doing, i.e. query for the entries related to the asset(s), not the other way around. You should be able to do this with a single <a href="https://craftcms.com/docs/4.x/entries.html#querying-entries" rel="nofollow noreferrer">entries query</a>:</p>
<pre class="lang-php prettyprint-override"><code>$pages = Entry::find()
    -&gt;section('pages')
    -&gt;relatedTo([
        'targetElement' =&gt; ['array of listed assets id'],
        'field' =&gt; 'featuredImage',
    ])
    -&gt;all();
</code></pre>

CC BY-SA 4.0
2022-12-27T19:31:04.333Z
2022-12-27T19:31:04.333Z
Mats Mikkel Rummelhoff
<p>I have a list of assets link with assets id.
eg: 23453 =&gt; 'https://some_cool_domain/assets/file_name.jpg'
I need to find all the pages where this asset is being used.</p>
<p>I am new to craftcms. I tried  to get all the entries from page section.</p>
<pre><code>$pages = Entry::find()
            // get all entry from page section
           -&gt;section('pages')
           -&gt;all(); 
</code></pre>
<p>Then i tried to get all the assets filter by my asset list (asset id). and put relation to pages.</p>
<pre><code> $assetsUsedInPages = Asset::find()
            -&gt;id(['array of listed assets id'])
            -&gt;relatedTo([
                'element' =&gt; $pages,
                'field' =&gt; 'featuredImage',  // image handler .
            ])-&gt;all();
</code></pre>
<p>I want to get slug or uri field , may be from which i can get the desired list of page.  but i am getting empty array of $pages.
I feel like i am going on the wrong way.  Any Suggestions please??</p>
<p>Thank you.</p>

<entries><assets><slug>
How to get list of pages where the asset is used?
# How to get list of pages where the asset is used?

(Asked by: username_0 on 2022-12-27)

I have a list of assets link with assets id.
eg: 23453 => 'https://some_cool_domain/assets/file_name.jpg'
I need to find all the pages where this asset is being used.
I am new to craftcms. I tried  to get all the entries from page section.
$pages = Entry::find()
            // get all entry from page section
           ->section('pages')
           ->all(); 

Then i tried to get all the assets filter by my asset list (asset id). and put relation to pages.
 $assetsUsedInPages = Asset::find()
            ->id(['array of listed assets id'])
            ->relatedTo([
                'element' => $pages,
                'field' => 'featuredImage',  // image handler .
            ])->all();

I want to get slug or uri field , may be from which i can get the desired list of page.  but i am getting empty array of $pages.
I feel like i am going on the wrong way.  Any Suggestions please??
Thank you.

**Tags:** entries, assets, slug


---

## 1 answers

### Answer by username_1 on 2022-12-27. Score: 1

You're close! From looking at your code, it seems like you're eventually querying for the related assets themselves though – not the entries those assets are related to. So essentially, you'll need to work backwards vs. what you're currently doing, i.e. query for the entries related to the asset(s), not the other way around. You should be able to do this with a single entries query:
$pages = Entry::find()
    ->section('pages')
    ->relatedTo([
        'targetElement' => ['array of listed assets id'],
        'field' => 'featuredImage',
    ])
    ->all();


---

