Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-14T10:46:36.840Z
Answers.Text: 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!
Answers.UserDisplayName: Andrew
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-14T18:35:13.913Z
Answers.Text: 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.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-17T13:33:40.483Z
Answers.Text: 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!
Answers.UserDisplayName: Andrew
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-17T18:16:11.000Z
Answers.Text: @Andrew If `file.fileTitle` is a plain text field, you should be able to simply do `file.fileTitle('*' ~ searchQuery ~ '*')` instead.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-18T11:16:43.893Z
Answers.Text: 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.
Answers.UserDisplayName: Andrew
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-18T19:16:38.467Z
Answers.Text: 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.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-13T21:33:29.820Z
Answers.LastActivityDate: 2022-01-13T21:41:04.153Z
Answers.LastEditDate: 2022-01-13T21:41:04.153Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <assets><search>
Title: Display entry field if it contains search query
ThreadText: # 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 %}
    <ul>
    {% for entry in entries %}
    <li><a href="{{ entry.url }}">{{ entry.title }}</a><br>
            {% 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 %}<br>
                  {% endfor %}
            {% endif %}   
  </li>
    {% endfor %}
    </ul>
  {% 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](https://craftcms.com/docs/3.x/element-queries.html) 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](https://craftcms.com/docs/3.x/assets.html#title) 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**: @username_0 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 username_1. 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.  
  
  ---
  
  