You can fetch users in your templates or PHP code using user queries.
::: code
{# Create a new user query #}
{% set myUserQuery = craft.users() %}// Create a new user query
$myUserQuery = \craft\elements\User::find();:::
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. :::
We can display a list of the users in an “Authors” user group by doing the following:
- Create a user query with
craft.users(). - Set the group parameter on it.
- Fetch the users with
.all(). - 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>User queries support the following parameters:
Narrows the query results to only users that have admin accounts.
::: code
{# Fetch admins #}
{% set elements = craft.queryFunction()
.admin()
.all() %}// Fetch admins
$elements = ElementClass::find()
->admin()
->all();:::
Clears out the status and enabledForSite() parameters.
::: code
{# Fetch all elements, regardless of status #}
{% set elements = craft.queryFunction()
.anyStatus()
.all() %}// Fetch all elements, regardless of status
$elements = ElementClass::find()
->anyStatus()
->all();:::
Causes the query to return matching elements as arrays of data, rather than ElementClass objects.
::: code
{# Fetch elements as arrays #}
{% set elements = craft.queryFunction()
.asArray()
.all() %}// Fetch elements as arrays
$elements = ElementClass::find()
->asArray()
->all();:::
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.
::: code
{# Fetch users that can access the Control Panel #}
{% set elements = craft.queryFunction()
.can('accessCp')
.all() %}// Fetch users that can access the Control Panel
$elements = ElementClass::find()
->can('accessCp')
->all();:::
Narrows the query results based on the elements’ creation dates.
Possible values include:
| Value | Fetches 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. |
::: code
{# 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() %}// Fetch elements created last month
$start = (new \DateTime('first day of last month'))->format(\DateTime::ATOM);
$end = (new \DateTime('first day of this month'))->format(\DateTime::ATOM);
$elements = ElementClass::find()
->dateCreated(['and', ">= {$start}", "< {$end}"])
->all();:::
Narrows the query results based on the elements’ last-updated dates.
Possible values include:
| Value | Fetches 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. |
::: code
{# Fetch elements updated in the last week #}
{% set lastWeek = date('1 week ago')|atom %}
{% set elements = craft.queryFunction()
.dateUpdated(">= #{lastWeek}")
.all() %}// Fetch elements updated in the last week
$lastWeek = (new \DateTime('1 week ago'))->format(\DateTime::ATOM);
$elements = ElementClass::find()
->dateUpdated(">= {$lastWeek}")
->all();:::
Narrows the query results to only drafts created by a given user.
Possible values include:
| Value | Fetches drafts… |
|---|---|
1 |
created by the user with an ID of 1. |
| a craft\elements\User object | by the user represented by the object. |
::: code
{# Fetch drafts by the current user #}
{% set elements = craft.queryFunction()
.draftCreator(currentUser)
.all() %}// Fetch drafts by the current user
$elements = ElementClass::find()
->draftCreator(Craft::$app->user->identity)
->all();:::
Narrows the query results based on the elements’ draft’s ID (from the drafts table).
Possible values include:
| Value | Fetches drafts… |
|---|---|
1 |
for the draft with an ID of 1. |
::: code
{# Fetch a draft #}
{% set elements = craft.queryFunction()
.draftId(10)
.all() %}// Fetch a draft
$elements = ElementClass::find()
->draftIf(10)
->all();:::
Narrows the query results to only drafts of a given element.
Possible values include:
| Value | Fetches drafts… |
|---|---|
1 |
for the element with an ID of 1. |
| a ElementClass object | for the element represented by the object. |
::: code
{# Fetch drafts of the element #}
{% set elements = craft.queryFunction()
.draftOf(myElement)
.all() %}// Fetch drafts of the element
$elements = ElementClass::find()
->draftOf($myElement)
->all();:::
Narrows the query results to only drafts elements.
::: code
{# Fetch a draft element #}
{% set elements = {twig-function}
.drafts()
.id(123)
.one() %}// Fetch a draft element
$elements = ElementClass::find()
->drafts()
->id(123)
->one();:::
Narrows the query results based on the users’ email addresses.
Possible values include:
| Value | Fetches 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. |
::: code
{# Fetch users with a .co.uk domain on their email address #}
{% set elements = craft.queryFunction()
.email('*.co.uk')
.all() %}// Fetch users with a .co.uk domain on their email address
$elements = ElementClass::find()
->email('*.co.uk')
->all();:::
Narrows the query results based on the users’ first names.
Possible values include:
| Value | Fetches elements… |
|---|---|
'Jane' |
with a first name of Jane. |
'not Jane' |
not with a first name of Jane. |
::: code
{# Fetch all the Jane's #}
{% set elements = craft.queryFunction()
.firstName('Jane')
.all() %}// Fetch all the Jane's
$elements = ElementClass::find()
->firstName('Jane')
->one();:::
Causes the query results to be returned in the order specified by id.
::: code
{# Fetch elements in a specific order #}
{% set elements = craft.queryFunction()
.id([1, 2, 3, 4, 5])
.fixedOrder()
.all() %}// Fetch elements in a specific order
$elements = ElementClass::find()
->id([1, 2, 3, 4, 5])
->fixedOrder()
->all();:::
Narrows the query results based on the user group the users belong to.
Possible values include:
| Value | Fetches 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 object | in a group represented by the object. |
::: code
{# Fetch elements in the Foo user group #}
{% set elements = craft.queryFunction()
.group('foo')
.all() %}// Fetch elements in the Foo user group
$elements = ElementClass::find()
->group('foo')
->all();:::
Narrows the query results based on the user group the users belong to, per the groups’ IDs.
Possible values include:
| Value | Fetches elements… |
|---|---|
1 |
in 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. |
::: code
{# Fetch elements in a group with an ID of 1 #}
{% set elements = craft.queryFunction()
.groupId(1)
.all() %}// Fetch elements in a group with an ID of 1
$elements = ElementClass::find()
->groupId(1)
->all();:::
Narrows the query results based on the elements’ IDs.
Possible values include:
| Value | Fetches elements… |
|---|---|
1 |
with 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. |
::: code
{# Fetch the element by its ID #}
{% set element = craft.queryFunction()
.id(1)
.one() %}// Fetch the element by its ID
$element = ElementClass::find()
->id(1)
->one();:::
::: tip This can be combined with fixedOrder if you want the results to be returned in a specific order. :::
Causes the query to return matching elements as they are stored in the database, ignoring matching placeholder elements that were set by craft\services\Elements::setPlaceholderElement().
Causes the query results to be returned in reverse order.
::: code
{# Fetch elements in reverse #}
{% set elements = craft.queryFunction()
.inReverse()
.all() %}// Fetch elements in reverse
$elements = ElementClass::find()
->inReverse()
->all();:::
Narrows the query results based on the users’ last login dates.
Possible values include:
| Value | Fetches 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. |
::: code
{# Fetch elements that logged in recently #}
{% set aWeekAgo = date('7 days ago')|atom %}
{% set elements = craft.queryFunction()
.lastLoginDate(">= #{aWeekAgo}")
.all() %}// Fetch elements that logged in recently
$aWeekAgo = (new \DateTime('7 days ago'))->format(\DateTime::ATOM);
$elements = ElementClass::find()
->lastLoginDate(">= {$aWeekAgo}")
->all();:::
Narrows the query results based on the users’ last names.
Possible values include:
| Value | Fetches elements… |
|---|---|
'Doe' |
with a last name of Doe. |
'not Doe' |
not with a last name of Doe. |
::: code
{# Fetch all the Doe's #}
{% set elements = craft.queryFunction()
.lastName('Doe')
.all() %}// Fetch all the Doe's
$elements = ElementClass::find()
->lastName('Doe')
->one();:::
Determines the number of elements that should be returned.
::: code
{# Fetch up to 10 elements #}
{% set elements = craft.queryFunction()
.limit(10)
.all() %}// Fetch up to 10 elements
$elements = ElementClass::find()
->limit(10)
->all();:::
Determines how many elements should be skipped in the results.
::: code
{# Fetch all elements except for the first 3 #}
{% set elements = craft.queryFunction()
.offset(3)
.all() %}// Fetch all elements except for the first 3
$elements = ElementClass::find()
->offset(3)
->all();:::
Determines the order that the elements should be returned in.
::: code
{# Fetch all elements in order of date created #}
{% set elements = craft.queryFunction()
.orderBy('dateCreated asc')
.all() %}// Fetch all elements in order of date created
$elements = ElementClass::find()
->orderBy('dateCreated asc')
->all();:::
If unique() is set, this determines which site should be selected when querying multi-site elements.
For example, if element “Foo” exists in Site A and Site B, and element “Bar” exists in Site B and Site C,
and this is set to ['c', 'b', 'a'], then Foo will be returned for Site C, and Bar will be returned
for Site B.
If this isn’t set, then preference goes to the current site.
::: code
{# Fetch unique elements from Site A, or Site B if they don’t exist in Site A #}
{% set elements = craft.queryFunction()
.site('*')
.unique()
.preferSites(['a', 'b'])
.all() %}// Fetch unique elements from Site A, or Site B if they don’t exist in Site A
$elements = ElementClass::find()
->site('*')
->unique()
->preferSites(['a', 'b'])
->all();:::
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.
::: code
{# Fetch all elements that are related to myCategory #}
{% set elements = craft.queryFunction()
.relatedTo(myCategory)
.all() %}// Fetch all elements that are related to $myCategory
$elements = ElementClass::find()
->relatedTo($myCategory)
->all();:::
Narrows the query results to only revisions created by a given user.
Possible values include:
| Value | Fetches revisions… |
|---|---|
1 |
created by the user with an ID of 1. |
| a craft\elements\User object | by the user represented by the object. |
::: code
{# Fetch revisions by the current user #}
{% set elements = craft.queryFunction()
.revisionCreator(currentUser)
.all() %}// Fetch revisions by the current user
$elements = ElementClass::find()
->revisionCreator(Craft::$app->user->identity)
->all();:::
Narrows the query results based on the elements’ revision’s ID (from the revisions table).
Possible values include:
| Value | Fetches revisions… |
|---|---|
1 |
for the revision with an ID of 1. |
::: code
{# Fetch a revision #}
{% set elements = craft.queryFunction()
.revisionId(10)
.all() %}// Fetch a revision
$elements = ElementClass::find()
->revisionIf(10)
->all();:::
Narrows the query results to only revisions of a given element.
Possible values include:
| Value | Fetches revisions… |
|---|---|
1 |
for the element with an ID of 1. |
| a ElementClass object | for the element represented by the object. |
::: code
{# Fetch revisions of the element #}
{% set elements = craft.queryFunction()
.revisionOf(myElement)
.all() %}// Fetch revisions of the element
$elements = ElementClass::find()
->revisionOf($myElement)
->all();:::
Narrows the query results to only revision elements.
::: code
{# Fetch a revision element #}
{% set elements = {twig-function}
.revisions()
.id(123)
.one() %}// Fetch a revision element
$elements = ElementClass::find()
->revisions()
->id(123)
->one();:::
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.
::: code
{# Get the search query from the 'q' query string param #}
{% set searchQuery = craft.app.request.getQueryParam('q') %}
{# Fetch all elements that match the search query #}
{% set elements = craft.queryFunction()
.search(searchQuery)
.all() %}// Get the search query from the 'q' query string param
$searchQuery = \Craft::$app->request->getQueryParam('q');
// Fetch all elements that match the search query
$elements = ElementClass::find()
->search($searchQuery)
->all();:::
Narrows the query results based on the elements’ statuses.
Possible values include:
| Value | Fetches elements… |
|---|---|
'active' (default) |
with active accounts. |
'suspended' |
with suspended accounts. |
'pending' |
with accounts that are still pending activation. |
'locked' |
with locked accounts (regardless of whether they’re active or suspended). |
['active', 'suspended'] |
with active or suspended accounts. |
::: code
{# Fetch active and locked elements #}
{% set elements = craft.queryFunction()
.status(['active', 'locked'])
.all() %}// Fetch active and locked elements
$elements = ElementClass::find()
->status(['active', 'locked'])
->all();:::
Narrows the query results to only elements that have been soft-deleted.
::: code
{# Fetch trashed elements #}
{% set elements = craft.queryFunction()
.trashed()
.all() %}// Fetch trashed elements
$elements = ElementClass::find()
->trashed()
->all();:::
Narrows the query results based on the elements’ UIDs.
::: code
{# Fetch the element by its UID #}
{% set element = craft.queryFunction()
.uid('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
.one() %}// Fetch the element by its UID
$element = ElementClass::find()
->uid('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
->one();:::
Narrows the query results based on the users’ usernames.
Possible values include:
| Value | Fetches elements… |
|---|---|
'foo' |
with a username of foo. |
'not foo' |
not with a username of foo. |
::: code
{# Get the requested username #}
{% set requestedUsername = craft.app.request.getSegment(2) %}
{# Fetch that user #}
{% set element = craft.queryFunction()
.username(requestedUsername|literal)
.one() %}// Get the requested username
$requestedUsername = \Craft::$app->request->getSegment(2);
// Fetch that user
$element = ElementClass::find()
->username(\craft\helpers\Db::escapeParam($requestedUsername))
->one();:::
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.
::: code
{# Fetch elements eager-loaded with the "Related" field’s relations #}
{% set elements = craft.queryFunction()
.with(['related'])
.all() %}// Fetch elements eager-loaded with the "Related" field’s relations
$elements = ElementClass::find()
->with(['related'])
->all();:::