<p>Like all relational fields, Assets fields return an <a href="https://craftcms.com/docs/3.x/element-queries.html" rel="nofollow noreferrer">ElementQuery</a> instance (element queries are basically pre-baked query builders for fetching elements). You can use that ElementQuery's <code>title()</code> parameter to narrow down results based on assets' titles, including fuzzy searching. Here's how you could query for only assets with a title that contains the search query:</p>
<pre><code>{% for asset in file.file.title('*' ~ searchQuery  ~ '*').all() %}
    {{ asset.title }}
{% endfor %}
</code></pre>
<p>Basically, the above appends <code>*</code> before and after the search query, which tells the ElementQuery returned by the Assets field (<code>file.file</code>, in your case) to only query for assets with titles that contain the search query somewhere.</p>
<p>There are other possible syntaxes available as well, depending on how you want to approach the title matching – see <a href="https://craftcms.com/docs/3.x/assets.html#title" rel="nofollow noreferrer">the official docs</a> for more info.</p>

CC BY-SA 4.0
2022-01-14T10:46:36.840Z
This works perfectly for the example above. But I've tried to use the same code for a different Matrix block and I'm struggling to get it to work. The Matrix field is similar, but uses a different handle. The block handle is 'file' and the field handle is 'fileTitle', so I'm assuming it should be:  {% for asset in file.fileTitle.title('*' ~ searchQuery  ~ '*').all() %}
    {{ asset.title }}
{% endfor %} - this doesn't work, so any help greatly appreciated!
Andrew
CC BY-SA 4.0
2022-01-14T18:35:13.913Z
You’re missing the asterisks before and after the searchQuery, unless that was just a typo in your comment. Without the asterisks, Craft will query for assets with a filetitle exactly equal to the search query, which I assume is not what you want.
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-01-17T13:33:40.483Z
My mistake, the second version is a Matrix block with a text field. So I need the search query for a text field not an asset field. Unfortunately I cannot find any information on this anywhere. Once again any help would be fantastic!
Andrew
CC BY-SA 4.0
2022-01-17T18:16:11.000Z
@Andrew If `file.fileTitle` is a plain text field, you should be able to simply do `file.fileTitle('*' ~ searchQuery ~ '*')` instead.
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-01-18T11:16:43.893Z
Many, many thanks for your help Mats. Unfortunately I'm a bit confused as the second version is not using a relational field. If I use `{% for file in file.fileTitle('*' ~ searchQuery  ~ '*').all() %}{{ file.fileTitle }} {% endfor %}` - it doesn't work. So I've still got the syntax wrong.
Andrew
CC BY-SA 4.0
2022-01-18T19:16:38.467Z
Perhaps it’d be clearer/easier to figure out if you could post the second version as a new question, with a complete code snippet.
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-01-13T21:33:29.820Z
2022-01-13T21:41:04.153Z
2022-01-13T21:41:04.153Z
Mats Mikkel Rummelhoff
<p>Is it possible to display an entry field in search results if it contains the search query?</p>
<p>Context: I have an entry with a list of PDF assets. I want to show an asset in the search results if it contains the search query in the asset title. By default all the asset titles would display if any one contains the search query.</p>
<p>This is what I have in mind:</p>
<pre><code>{% set searchQuery = craft.app.request.getParam('q') %}
{% set entries = craft.entries
  .section([
  'and', 'resources'
  ])
  .search(searchQuery)
  .orderBy('score')
  .all() %}
{% if entries|length %}
  &lt;ul&gt;
  {% for entry in entries %}
  &lt;li&gt;&lt;a href=&quot;{{ entry.url }}&quot;&gt;{{ entry.title }}&lt;/a&gt;&lt;br&gt;
          {% set block = entry.downloads.all() %}   
          {% if block|length %}
                {% for file in block %}
                  {% for asset in file.file.all() %}
                  
                  {% if asset ***CONTAINS SEARCHQUERY*** %}
                      {{ asset.title }}
                  {% endif %}   
                  
                  {% endfor %}&lt;br&gt;
                {% endfor %}
          {% endif %}   
&lt;/li&gt;
  {% endfor %}
  &lt;/ul&gt;
{% endif %}
</code></pre>

<assets><search>
Display entry field if it contains search query
# Display entry field if it contains search query

(Asked by: username_0 on 2022-01-13)

Is it possible to display an entry field in search results if it contains the search query?
Context: I have an entry with a list of PDF assets. I want to show an asset in the search results if it contains the search query in the asset title. By default all the asset titles would display if any one contains the search query.
This is what I have in mind:
{% set searchQuery = craft.app.request.getParam('q') %}
{% set entries = craft.entries
  .section([
  'and', 'resources'
  ])
  .search(searchQuery)
  .orderBy('score')
  .all() %}
{% if entries|length %}
  
  {% for entry in entries %}
  {{ entry.title }}
          {% set block = entry.downloads.all() %}   
          {% if block|length %}
                {% for file in block %}
                  {% for asset in file.file.all() %}
                  
                  {% if asset ***CONTAINS SEARCHQUERY*** %}
                      {{ asset.title }}
                  {% endif %}   
                  
                  {% endfor %}
                {% endfor %}
          {% endif %}   

  {% endfor %}
  
{% endif %}

**Tags:** assets, search

---

## 1 answers

### Answer by username_1 on 2022-01-13. Score: 0 | Accepted answer

Like all relational fields, Assets fields return an ElementQuery instance (element queries are basically pre-baked query builders for fetching elements). You can use that ElementQuery's title() parameter to narrow down results based on assets' titles, including fuzzy searching. Here's how you could query for only assets with a title that contains the search query:
{% for asset in file.file.title('*' ~ searchQuery  ~ '*').all() %}
    {{ asset.title }}
{% endfor %}

Basically, the above appends * before and after the search query, which tells the ElementQuery returned by the Assets field (file.file, in your case) to only query for assets with titles that contain the search query somewhere.
There are other possible syntaxes available as well, depending on how you want to approach the title matching – see the official docs for more info.

> **Comments:**
> **username_0**: This works perfectly for the example above. But I've tried to use the same code for a different Matrix block and I'm struggling to get it to work. The Matrix field is similar, but uses a different handle. The block handle is 'file' and the field handle is 'fileTitle', so I'm assuming it should be:  {% for asset in file.fileTitle.title('*' ~ searchQuery  ~ '*').all() %}     {{ asset.title }} {% endfor %} - this doesn't work, so any help greatly appreciated!  
> **username_1**: You’re missing the asterisks before and after the searchQuery, unless that was just a typo in your comment. Without the asterisks, Craft will query for assets with a filetitle exactly equal to the search query, which I assume is not what you want.  
> **username_0**: My mistake, the second version is a Matrix block with a text field. So I need the search query for a text field not an asset field. Unfortunately I cannot find any information on this anywhere. Once again any help would be fantastic!  
> **username_1**: @Andrew If `file.fileTitle` is a plain text field, you should be able to simply do `file.fileTitle('*' ~ searchQuery ~ '*')` instead.  
> **username_0**: Many, many thanks for your help Mats. Unfortunately I'm a bit confused as the second version is not using a relational field. If I use `{% for file in file.fileTitle('*' ~ searchQuery  ~ '*').all() %}{{ file.fileTitle }} {% endfor %}` - it doesn't work. So I've still got the syntax wrong.  
> **username_1**: Perhaps it’d be clearer/easier to figure out if you could post the second version as a new question, with a complete code snippet.  

---

