User Queries

You can fetch users in your templates or PHP code using user queries.

{# Create a new user query #}
{% set myUserQuery = craft.users() %}

Once you’ve created a user query, you can set parameters on it to narrow down the results, and then execute it by calling .all(). An array of User objects will be returned.

TIP

See Introduction to Element Queries to learn about how element queries work.

Example

We can display a list of the users in an “Authors” user group by doing the following:

  1. Create a user query with craft.users().
  2. Set the group parameter on it.
  3. Fetch the users with .all().
  4. Loop through the users using a for tag to create the list HTML.
{# Create a user query with the 'group' parameter #}
{% set myUserQuery = craft.users()
    .group('authors') %}

{# Fetch the users #}
{% set users = myUserQuery.all() %}

{# Display the list #}
<ul>
    {% for user in users %}
        <li><a href="{{ url('authors/'~user.username) }}">{{ user.name }}</a></li>
    {% endfor %}
</ul>

Parameters

User queries support the following parameters:

admin

Narrows the query results to only users that have admin accounts.

{# Fetch admins #}
{% set elements = {twig-function}
    .admin()
    .all() %}

anyStatus

Clears out the status and enabledForSite() parameters.

{# Fetch all elements, regardless of status #}
{% set elements = craft.queryFunction()
    .anyStatus()
    .all() %}

asArray

Causes the query to return matching elements as arrays of data, rather than ElementClass objects.

{# Fetch elements as arrays #}
{% set elements = craft.queryFunction()
    .asArray()
    .all() %}

can

Narrows the query results to only users that have a certain user permission, either directly on the user account or through one of their user groups.

See Users for a full list of available user permissions defined by Craft.

{# Fetch users that can access the Control Panel #}
{% set elements = {twig-function}
    .can('accessCp')
    .all() %}

dateCreated

Narrows the query results based on the elements’ creation dates.

Possible values include:

ValueFetches elements…
'>= 2018-04-01'that were created on or after 2018-04-01.
'< 2018-05-01'that were created before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01']that were created between 2018-04-01 and 2018-05-01.
{# Fetch elements created last month #}
{% set start = date('first day of last month')|atom %}
{% set end = date('first day of this month')|atom %}

{% set elements = craft.queryFunction()
    .dateCreated(['and', ">= #{start}", "< #{end}"])
    .all() %}

dateUpdated

Narrows the query results based on the elements’ last-updated dates.

Possible values include:

ValueFetches elements…
'>= 2018-04-01'that were updated on or after 2018-04-01.
'< 2018-05-01'that were updated before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01']that were updated between 2018-04-01 and 2018-05-01.
{# Fetch elements updated in the last week #}
{% set lastWeek = date('1 week ago')|atom %}

{% set elements = craft.queryFunction()
    .dateUpdated(">= #{lastWeek}")
    .all() %}

email

Narrows the query results based on the users’ email addresses.

Possible values include:

ValueFetches elements…
'foo@bar.baz'with an email of foo@bar.baz.
'not foo@bar.baz'not with an email of foo@bar.baz.
'*@bar.baz'with an email that ends with @bar.baz.
{# Fetch users with a .co.uk domain on their email address #}
{% set elements = craft.queryFunction()
    .email('*.co.uk')
    .all() %}

firstName

Narrows the query results based on the users’ first names.

Possible values include:

ValueFetches elements…
'Jane'with a first name of Jane.
'not Jane'not with a first name of Jane.
{# Fetch all the Jane's #}
{% set elements = craft.queryFunction()
    .firstName('Jane')
    .all() %}

fixedOrder

Causes the query results to be returned in the order specified by id.

{# Fetch elements in a specific order #}
{% set elements = craft.queryFunction()
    .id([1, 2, 3, 4, 5])
    .fixedOrder()
    .all() %}

group

Narrows the query results based on the user group the users belong to.

Possible values include:

ValueFetches elements…
'foo'in a group with a handle of foo.
'not foo'not in a group with a handle of foo.
['foo', 'bar']in a group with a handle of foo or bar.
['not', 'foo', 'bar']not in a group with a handle of foo or bar.
a UserGroup objectin a group represented by the object.
{# Fetch elements in the Foo user group #}
{% set elements = craft.queryFunction()
    .group('foo')
    .all() %}

groupId

Narrows the query results based on the user group the users belong to, per the groups’ IDs.

Possible values include:

ValueFetches elements…
1in a group with an ID of 1.
'not 1'not in a group with an ID of 1.
[1, 2]in a group with an ID of 1 or 2.
['not', 1, 2]not in a group with an ID of 1 or 2.
{# Fetch elements in a group with an ID of 1 #}
{% set elements = craft.queryFunction()
    .groupId(1)
    .all() %}

id

Narrows the query results based on the elements’ IDs.

Possible values include:

ValueFetches elements…
1with an ID of 1.
'not 1'not with an ID of 1.
[1, 2]with an ID of 1 or 2.
['not', 1, 2]not with an ID of 1 or 2.
{# Fetch the element by its ID #}
{% set element = craft.queryFunction()
    .id(1)
    .one() %}

TIP

This can be combined with fixedOrder if you want the results to be returned in a specific order.

inReverse

Causes the query results to be returned in reverse order.

{# Fetch elements in reverse #}
{% set elements = craft.queryFunction()
    .inReverse()
    .all() %}

lastLoginDate

Narrows the query results based on the users’ last login dates.

Possible values include:

ValueFetches elements…
'>= 2018-04-01'that last logged-in on or after 2018-04-01.
'< 2018-05-01'that last logged-in before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01']that last logged-in between 2018-04-01 and 2018-05-01.
{# Fetch elements that logged in recently #}
{% set aWeekAgo = date('7 days ago')|atom %}

{% set elements = craft.queryFunction()
    .lastLoginDate(">= #{aWeekAgo}")
    .all() %}

lastName

Narrows the query results based on the users’ last names.

Possible values include:

ValueFetches elements…
'Doe'with a last name of Doe.
'not Doe'not with a last name of Doe.
{# Fetch all the Doe's #}
{% set elements = craft.queryFunction()
    .lastName('Doe')
    .all() %}

limit

Determines the number of elements that should be returned.

{# Fetch up to 10 elements  #}
{% set elements = craft.queryFunction()
    .limit(10)
    .all() %}

offset

Determines how many elements should be skipped in the results.

{# Fetch all elements except for the first 3 #}
{% set elements = craft.queryFunction()
    .offset(3)
    .all() %}

orderBy

Determines the order that the elements should be returned in.

{# Fetch all elements in order of date created #}
{% set elements = craft.queryFunction()
    .orderBy('elements.dateCreated asc')
    .all() %}

relatedTo

Narrows the query results to only elements that are related to certain other elements.

See Relations for a full explanation of how to work with this parameter.

{# Fetch all elements that are related to myCategory #}
{% set elements = craft.queryFunction()
    .relatedTo(myCategory)
    .all() %}

Narrows the query results to only elements that match a search query.

See Searching for a full explanation of how to work with this parameter.

{# Get the search query from the 'q' query string param #}
{% set searchQuery = craft.request.getQueryParam('q') %}

{# Fetch all elements that match the search query #}
{% set elements = craft.queryFunction()
    .search(searchQuery)
    .all() %}

status

Narrows the query results based on the elements’ statuses.

Possible values include:

ValueFetches elements…
'active'(default)with active accounts.
'locked'with locked accounts.
'suspended'with suspended accounts.
'pending'with accounts that are still pending activation.
['active', 'locked']with active or locked accounts.
{# Fetch active and locked elements #}
{% set elements = {twig-function}
    .status(['active', 'locked'])
    .all() %}

uid

Narrows the query results based on the elements’ UIDs.

{# Fetch the element by its UID #}
{% set element = craft.queryFunction()
    .uid('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
    .one() %}

username

Narrows the query results based on the users’ usernames.

Possible values include:

ValueFetches elements…
'foo'with a username of foo.
'not foo'not with a username of foo.
{# Get the requested username #}
{% set requestedUsername = craft.app.request.getSegment(2) %}

{# Fetch that user #}
{% set element = craft.queryFunction()
    .username(requestedUsername|literal)
    .one() %}

with

Causes the query to return matching elements eager-loaded with related elements.

See Eager-Loading Elements for a full explanation of how to work with this parameter.

{# Fetch elements eager-loaded with the "Related" field’s relations #}
{% set elements = craft.queryFunction()
    .with(['related'])
    .all() %}