<p><strong>Caveat</strong>: This is totally untested, and could be nonsense. I'm sure you'll let me know. It assumes that the <a href="https://craftcms.com/docs/3.x/element-queries.html#executing-element-queries" rel="nofollow noreferrer">Craft element query</a> methods can be used here.</p>
<p>If you have a 'lang' value, add a <em>where</em> to your basic assets element query</p>
<pre><code>    {% set folderId = craft.app.request.getQueryParam('folderId') %}
    {% set query = craft.app.request.getQueryParam('q') %}
    {% set lang = craft.app.request.getQueryParam('lang') %}
    
    {# Search for assets in the downloads folder #}
    {% set assetsQuery = craft.assets().volume('downloads') %}
    {% set assetsQuery = assets.folderId(folderId).includeSubFolders() %}

    {# If there's a lang value, add a where to the element query #}
    {% if lang is not empty %}
    {%     set assetsQuery = assetsQuery.where(expression('[[field_lang]] = :lang', {lang: lang})) %}
    {% endif %}
    
    {% set searchQuery = [] %}

    {# Check if a query was supplied, if so then search for it. #}
    {% if query is not empty %}
    {%     set searchQuery = searchQuery|merge({query: query}) %}
    {% endif %}
    
    {% paginate assetsQuery.search(searchQuery).limit(10) as pageInfo, pageEntries %}
</code></pre>
<p>*Note that I've changed the variable 'assets' to 'assetsQuery'</p>

CC BY-SA 4.0
2021-05-10T12:26:08.953Z
Got an exception Twig\Error\SyntaxError: An array element must be followed by a comma. Unexpected token "arrow function" of value "=>" ("punctuation" expected with value ","). Looks like the arrow function in the where clause isn't allowed. We are on Craft version 3.6.13
Guido Neele
CC BY-SA 4.0
2021-05-10T13:45:31.320Z
Looks like it needs to be an [Expression](https://craftcms.com/docs/3.x/dev/functions.html#expression), so try
`set assetsQuery = assetsQuery.where( expression('[[lang]] = :lang', {lang: lang}) );`
Wallace
CC BY-SA 4.0
2021-05-10T19:47:56.093Z
Thanks Andy, this is working great. Had to change [[lang]] to [[field_lang]] and it worked.
Guido Neele
CC BY-SA 4.0
2021-05-06T15:05:25.727Z
2021-05-11T11:22:56.710Z
2021-05-11T11:22:56.710Z
Wallace
<p>How can I search for assets containing a custom field? The field on the asset is called <b>lang</b>. When I use this code no assets are found.</p>
<pre><code>{% set folderId = craft.app.request.getQueryParam('folderId') %}
{% set query = craft.app.request.getQueryParam('q') %}
{% set lang = craft.app.request.getQueryParam('lang') %}

{# Search for assets in the downlaoads folder #}
{% set assets = craft.assets().volume('downloads') %}
{% set assets = assets.folderId(folderId).includeSubFolders() %}

{% set searchQuery = [] %}

{# Check if a query was supplied, if so then search for it. #}
{% if query is not empty %}
{%     set searchQuery = searchQuery|merge({query: query}) %}
{% endif %}

<b>{% if lang is not empty %}
{%   set searchQuery = searchQuery|merge({query: 'lang:' ~ lang}) %}
{% endif %}</b>

{% paginate assets.search(searchQuery).limit(10) as pageInfo, pageEntries %}</code></pre>
<p>I also tried:</p>
<pre><code>{% set query = query ~ ' lang: ' ~lang %}
{% paginate assets.search(query).limit(10) as pageInfo, pageEntries %}
</code></pre>
<p>If I remove the lang part results will show up.</p>
<p>The field is a Dropdown field with languages. Value is ISO code and Label the language name. The variable lang in the code contains nl, en etc.</p>

CC BY-SA 4.0
2021-05-06T11:04:13.933Z
Is your custom field searchable?
Wallace
CC BY-SA 4.0
2021-05-06T11:38:28.117Z
I checked 'Use this field’s values as search keywords' but that didn't help. The field is a Dropdown field with languages. Value is ISO code and Label the language name. The variable lang in the code contains nl, en etc.
Guido Neele
CC BY-SA 4.0
2021-05-06T14:38:18.557Z
If your values are known/fixed then I'd consider adding that as an element query, then searching on the results of that?
Wallace
<templating><assets><search><assetfilemodel>
Search for assets containing a custom field
# Search for assets containing a custom field

(Asked by: username_0 on 2021-05-06)

How can I search for assets containing a custom field? The field on the asset is called lang. When I use this code no assets are found.
{% set folderId = craft.app.request.getQueryParam('folderId') %}
{% set query = craft.app.request.getQueryParam('q') %}
{% set lang = craft.app.request.getQueryParam('lang') %}

{# Search for assets in the downlaoads folder #}
{% set assets = craft.assets().volume('downloads') %}
{% set assets = assets.folderId(folderId).includeSubFolders() %}

{% set searchQuery = [] %}

{# Check if a query was supplied, if so then search for it. #}
{% if query is not empty %}
{%     set searchQuery = searchQuery|merge({query: query}) %}
{% endif %}

{% if lang is not empty %}
{%   set searchQuery = searchQuery|merge({query: 'lang:' ~ lang}) %}
{% endif %}

{% paginate assets.search(searchQuery).limit(10) as pageInfo, pageEntries %}
I also tried:
{% set query = query ~ ' lang: ' ~lang %}
{% paginate assets.search(query).limit(10) as pageInfo, pageEntries %}

If I remove the lang part results will show up.
The field is a Dropdown field with languages. Value is ISO code and Label the language name. The variable lang in the code contains nl, en etc.

**Tags:** templating, assets, search, assetfilemodel
> **Comments:**
> **username_1**: Is your custom field searchable?  
> **username_0**: I checked 'Use this field’s values as search keywords' but that didn't help. The field is a Dropdown field with languages. Value is ISO code and Label the language name. The variable lang in the code contains nl, en etc.  
> **username_1**: If your values are known/fixed then I'd consider adding that as an element query, then searching on the results of that?  

---

## 1 answers

### Answer by username_1 on 2021-05-06. Score: 1 | Accepted answer

Caveat: This is totally untested, and could be nonsense. I'm sure you'll let me know. It assumes that the Craft element query methods can be used here.
If you have a 'lang' value, add a where to your basic assets element query
    {% set folderId = craft.app.request.getQueryParam('folderId') %}
    {% set query = craft.app.request.getQueryParam('q') %}
    {% set lang = craft.app.request.getQueryParam('lang') %}
    
    {# Search for assets in the downloads folder #}
    {% set assetsQuery = craft.assets().volume('downloads') %}
    {% set assetsQuery = assets.folderId(folderId).includeSubFolders() %}

    {# If there's a lang value, add a where to the element query #}
    {% if lang is not empty %}
    {%     set assetsQuery = assetsQuery.where(expression('[[field_lang]] = :lang', {lang: lang})) %}
    {% endif %}
    
    {% set searchQuery = [] %}

    {# Check if a query was supplied, if so then search for it. #}
    {% if query is not empty %}
    {%     set searchQuery = searchQuery|merge({query: query}) %}
    {% endif %}
    
    {% paginate assetsQuery.search(searchQuery).limit(10) as pageInfo, pageEntries %}

*Note that I've changed the variable 'assets' to 'assetsQuery'

> **Comments:**
> **username_0**: Got an exception Twig\Error\SyntaxError: An array element must be followed by a comma. Unexpected token "arrow function" of value "=>" ("punctuation" expected with value ","). Looks like the arrow function in the where clause isn't allowed. We are on Craft version 3.6.13  
> **username_1**: Looks like it needs to be an [Expression](https://craftcms.com/docs/3.x/dev/functions.html#expression), so try `set assetsQuery = assetsQuery.where( expression('[[lang]] = :lang', {lang: lang}) );`  
> **username_0**: Thanks Andy, this is working great. Had to change [[lang]] to [[field_lang]] and it worked.  

---

