I have the below. I want to only show results that contain the query in the the username or fullName attributes.

Because currently if I search for say Tom the search results will return users without a username or full name containing the query Tom, but will show a person if the email contains it. I don't want this as emails are not publicly shown and therefore may cause confusion to the user searching.

How would I exclude email from the search criteria or only allow searching from the username or fullName attribute?

The same happens when searching entries - how do I narrow the search to only use the title field for example otherwise the search results can be too broad which is bad for UX.

{% set users = craft.users()
    .all() %}

{% if users|length %}
    <p>{{users|length }} results:</p>

        {% for user in users %}
           <li><a href="/people/{{ user.username }}">{{ user.username }}</a></li>
        {% endfor %}
{% else %}
    <p>Your search for {{ query }} didn&#39t return any users.</p>
{% endif %}

1 Answer 1


You can just limit your search to those specific user fields.

It should go something like:

{% set users = craft.users()
    .search('fullName:' ~ query ~ ' OR username:' ~ query)
    .all() %}

More info on searching here: https://docs.craftcms.com/v3/searching.html#supported-syntaxes

Top tip: You can test your search query directly in the search bar within Craft's admin. e.g. See if fullName:Tom OR username:Tom pulls the intended results.

  • 1
    Awesome that seem's to work perfectly! I had something similar but it didnt work as expected first time around. Appreciate it
    – Tom Byrom
    Commented Jun 17, 2020 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.