<p>You can use <code>craft.assets()</code> to search for assets, the same way you can use <code>craft.entries()</code> to find entries. See <a href="https://craftcms.com/docs/3.x/assets.html#querying-assets" rel="nofollow noreferrer">Querying Assets</a> for details. Asset queries allow you to use mostly the same methods that entry queries do. For example, you can also use the <a href="https://craftcms.com/docs/3.x/assets.html#search" rel="nofollow noreferrer"><code>.search</code> method</a> to search for keywords in the asset's title and searchable fields.</p>
<pre><code>{% set searchQuery = craft.app.request.getQueryParam('q') %}

{% set foundAssets = craft.assets()
    .search(searchQuery)
    .all()
%}
</code></pre>
<p>Limiting the search to assets that are selected on a specific entry can be done using the <a href="https://craftcms.com/docs/3.x/relations.html" rel="nofollow noreferrer">relatedTo</a> method:</p>
<pre><code>{% set downloadsPage = craft.entries().section('downloads').one() %}
{% set foundAssets = craft.assets()
    .search(searchQuery)
    .relatedTo(downloadsPage)
    .all()
%}
</code></pre>
<p>See the documentation linked above for more information (for example, how to limit the relation to a specific field and direction).</p>

CC BY-SA 4.0
2021-09-17T14:08:25.303Z
2021-09-17T14:08:25.303Z
MoritzLost
<p>I build a search component, and it works fine.
Now I would like to show all the uploaded assets from a specific page (&quot;Downloads&quot;) in the search results that include the search key.</p>
<p>How can I do that with Craft?</p>

<craft3><search>
Search query for assets
# Search query for assets

(Asked by: username_0 on 2021-09-17)

I build a search component, and it works fine.
Now I would like to show all the uploaded assets from a specific page ("Downloads") in the search results that include the search key.

How can I do that with Craft?

**Tags:** craft3, search


---

## 1 answers

### Answer by username_1 on 2021-09-17. Score: 2 | Accepted answer

You can use craft.assets() to search for assets, the same way you can use craft.entries() to find entries. See Querying Assets for details. Asset queries allow you to use mostly the same methods that entry queries do. For example, you can also use the .search method to search for keywords in the asset's title and searchable fields.
{% set searchQuery = craft.app.request.getQueryParam('q') %}

{% set foundAssets = craft.assets()
    .search(searchQuery)
    .all()
%}

Limiting the search to assets that are selected on a specific entry can be done using the relatedTo method:
{% set downloadsPage = craft.entries().section('downloads').one() %}
{% set foundAssets = craft.assets()
    .search(searchQuery)
    .relatedTo(downloadsPage)
    .all()
%}

See the documentation linked above for more information (for example, how to limit the relation to a specific field and direction).


---

