Dot All Lisbon – the official Craft CMS conference – is happening September 23 - 25.

Users

Users are Craft’s representation of people.

Each user has an email address and username by default, as well as optional fields for a name, photo, and password. Like other elements, users support custom fields by way of a field layout.

There are also preferences for localization, accessibility, and debugging that may be relevant depending on how you build your site and whether you grant the user access to the control panel.

What a user can do is determined by which groups they belong to, and what individual permissions they have been granted.

Active and Inactive Users

A Craft user can be active or inactive, meaning they have a credentialed account or simply exist as a record in the system. Navigate to the Users screen and you’ll find these represented by Credentialed and Inactive groupings under the “Account Type” heading.

You’ll most likely be creating active user accounts for content managers or site members to log in and do things. Each active account has its own status to describe that user’s level of access to the system:

  • Active – Account was activated by an admin, or the user set credentials to gain system access.
  • Pending – User was invited to activate their account but hasn’t completed the process.
  • Suspended – Account’s system access was explicitly revoked by another user with sufficient permissions.
  • Locked – Account was locked because of too many failed login attempts per the config4:maxInvalidLogins and config4:cooldownDuration config settings.
  • Inactive – Account never had credentials, or was explicitly deactivated.

You can’t create an inactive user from the control panel, but you can deactivate a user account by choosing Deactivate... from its action menu (). Inactive user accounts are best suited for specific circumstances, like Craft Commerce guest customers or an imaginary Craft-based CRM that manages contacts.

Addresses

Users each have an address book. Addresses can be managed on behalf of a user via the control panel, or by the user themselves.

Querying Users

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

{# 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.

See 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 #}
  {% for user in users %}
    <li><a href="{{ url('authors/'~user.username) }}">{{ user.name }}</a></li>
  {% endfor %}

Parameters

User queries support the following parameters:

| Param | Description | ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | admin | Narrows the query results to only users that have admin accounts. | affiliatedSite | Narrows the query results based on the users’ affiliated sites. | affiliatedSiteId | Narrows the query results based on the users’ affiliated sites, per the site’s ID(s). | andNotRelatedTo | Narrows the query results to only users that are not related to certain other elements. | andRelatedTo | Narrows the query results to only users that are related to certain other elements. | andWith | Causes the query to return matching users eager-loaded with related elements, in addition to the elements that were already specified by with. | asArray | Causes the query to return matching users as arrays of data, rather than User objects. | assetUploaders | Narrows the query results to only users that have uploaded an asset. | authorOf | Narrows the query results to users who are the author of the given entry. | authors | Narrows the query results to only users that are authors of an entry. | cache | Enables query cache for this Query. | 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. | canonicalsOnly | Narrows the query results to only canonical elements, including elements that reference another canonical element via canonicalId so long as they aren’t a draft. | clearCachedResult | Clears the cached result. | dateCreated | Narrows the query results based on the users’ creation dates. | dateUpdated | Narrows the query results based on the users’ last-updated dates. | eagerly | Causes the query to be used to eager-load results for the query’s source element and any other elements in its collection. | email | Narrows the query results based on the users’ email addresses. | firstName | Narrows the query results based on the users’ first names. | fixedOrder | Causes the query results to be returned in the order specified by id. | fullName | Narrows the query results based on the users’ full names. | getFieldLayouts | Returns the field layouts that could be associated with the resulting elements. | group | Narrows the query results based on the user group the users belong to. | groupId | Narrows the query results based on the user group the users belong to, per the groups’ IDs. | hasPhoto | Narrows the query results to only users that have (or don’t have) a user photo. | id | Narrows the query results based on the users’ IDs. | ignorePlaceholders | Causes the query to return matching users as they are stored in the database, ignoring matching placeholder elements that were set by craft\services\Elements::setPlaceholderElement(). | inBulkOp | Narrows the query results to only users that were involved in a bulk element operation. | inReverse | Causes the query results to be returned in reverse order. | language | Determines which site(s) the users should be queried in, based on their language. | lastLoginDate | Narrows the query results based on the users’ last login dates. | lastName | Narrows the query results based on the users’ last names. | limit | Determines the number of users that should be returned. | notRelatedTo | Narrows the query results to only users that are not related to certain other elements. | offset | Determines how many users should be skipped in the results. | orderBy | Determines the order that the users should be returned in. (If empty, defaults to username ASC.) | preferSites | If unique() is set, this determines which site should be selected when querying multi-site elements. | prepForEagerLoading | Prepares the query for lazy eager loading. | prepareSubquery | Prepares the element query and returns its subquery (which determines what elements will be returned). | relatedTo | Narrows the query results to only users that are related to certain other elements. | render | Executes the query and renders the resulting elements using their partial templates. | search | Narrows the query results to only users that match a search query. | siteSettingsId | Narrows the query results based on the users’ IDs in the elements_sites table. | status | Narrows the query results based on the users’ statuses. | trashed | Narrows the query results to only users that have been soft-deleted. | uid | Narrows the query results based on the users’ UIDs. | username | Narrows the query results based on the users’ usernames. | wasCountEagerLoaded | Returns whether the query result count was already eager loaded by the query's source element. | wasEagerLoaded | Returns whether the query results were already eager loaded by the query's source element. | with | Causes the query to return matching users eager-loaded with related elements. | withCustomFields | Sets whether custom fields should be factored into the query. | withGroups | Causes the query to return matching users eager-loaded with their user groups. | withProvisionalDrafts | Causes the query to return provisional drafts for the matching elements, when they exist for the current user.

admin

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

::: code

{# Fetch admins #}
{% set users = craft.users()
  .admin()
  .all() %}
// Fetch admins
$users = \craft\elements\User::find()
    ->admin()
    ->all();

:::

affiliatedSite

Narrows the query results based on the users’ affiliated sites.

Possible values include:

| Value | Fetches users… | - | - | 'foo' | affiliated with the site with a handle of foo. | ['foo', 'bar'] | affiliated with a site with a handle of foo or bar. | ['not', 'foo', 'bar'] | not affiliated with a site with a handle of foo or bar. | a craft\models\Site object | affiliated with the site represented by the object. | '*' | affiliated with any site.

::: code

{# Fetch users affiliated with the Foo site #}
{% set users = craft.users()
  .affiliatedSite('foo')
  .all() %}
// Fetch users affiliated with the Foo site
$users = \craft\elements\User::find()
    ->affiliatedSite('foo')
    ->all();

:::

affiliatedSiteId

Narrows the query results based on the users’ affiliated sites, per the site’s ID(s).

Possible values include:

| Value | Fetches users… | - | - | 1 | affiliated with the site with an ID of 1. | [1, 2] | affiliated with a site with an ID of 1 or 2. | ['not', 1, 2] | not affiliated with a site with an ID of 1 or 2. | '*' | affiliated with any site.

::: code

{# Fetch users affiliated with the site with an ID of 1 #}
{% set users = craft.users()
  .affiliatedSiteId(1)
  .all() %}
// Fetch users affiliated with the site with an ID of 1
$users = \craft\elements\User::find()
    ->affiliatedSiteId(1)
    ->all();

:::

andNotRelatedTo

Defined by craft\elements\db\ElementQuery

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

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

::: code

{# Fetch all users that are related to myCategoryA and not myCategoryB #}
{% set users = craft.users()
  .relatedTo(myCategoryA)
  .andNotRelatedTo(myCategoryB)
  .all() %}
// Fetch all users that are related to $myCategoryA and not $myCategoryB
$users = \craft\elements\User::find()
    ->relatedTo($myCategoryA)
    ->andNotRelatedTo($myCategoryB)
    ->all();

:::

andRelatedTo

Defined by craft\elements\db\ElementQuery

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

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

::: code

{# Fetch all users that are related to myCategoryA and myCategoryB #}
{% set users = craft.users()
  .relatedTo(myCategoryA)
  .andRelatedTo(myCategoryB)
  .all() %}
// Fetch all users that are related to $myCategoryA and $myCategoryB
$users = \craft\elements\User::find()
    ->relatedTo($myCategoryA)
    ->andRelatedTo($myCategoryB)
    ->all();

:::

andWith

Defined by craft\elements\db\ElementQuery

Causes the query to return matching users eager-loaded with related elements, in addition to the elements that were already specified by with.

.

asArray

Defined by craft\elements\db\ElementQuery

Causes the query to return matching users as arrays of data, rather than User objects.

::: code

{# Fetch users as arrays #}
{% set users = craft.users()
  .asArray()
  .all() %}
// Fetch users as arrays
$users = \craft\elements\User::find()
    ->asArray()
    ->all();

:::

assetUploaders

Narrows the query results to only users that have uploaded an asset.

::: code

{# Fetch all users who have uploaded an asset #}
{% set users = craft.users()
  .assetUploaders()
  .all() %}
// Fetch all users who have uploaded an asset
$users = \craft\elements\User::find()
    ->assetUploaders()
    ->all();

:::

authorOf

Narrows the query results to users who are the author of the given entry.

authors

Narrows the query results to only users that are authors of an entry.

::: code

{# Fetch authors #}
{% set users = craft.users()
  .authors()
  .all() %}
// Fetch authors
$users = \craft\elements\User::find()
    ->authors()
    ->all();

:::

cache

Defined by craft\elements\db\ElementQuery

Enables query cache for this Query.

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 User Management for a full list of available user permissions defined by Craft.

::: code

{# Fetch users who can access the front end when the system is offline #}
{% set users = craft.users()
  .can('accessSiteWhenSystemIsOff')
  .all() %}
// Fetch users who can access the front end when the system is offline
$users = \craft\elements\User::find()
    ->can('accessSiteWhenSystemIsOff')
    ->all();

:::

canonicalsOnly

Defined by craft\elements\db\ElementQuery

Narrows the query results to only canonical elements, including elements that reference another canonical element via canonicalId so long as they aren’t a draft.

Unpublished drafts can be included as well if drafts(null) and draftOf(false) are also passed.

clearCachedResult

Defined by craft\elements\db\ElementQuery

Clears the cached result.

dateCreated

Defined by craft\elements\db\ElementQuery

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

Possible values include:

| Value | Fetches users… | - | - | '>= 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. | now/today/tomorrow/yesterday | that were created at midnight of the specified relative date.

::: code

{# Fetch users created last month #}
{% set start = date('first day of last month')|atom %}
{% set end = date('first day of this month')|atom %}

{% set users = craft.users()
  .dateCreated(['and', ">= #{start}", "< #{end}"])
  .all() %}
// Fetch users 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);

$users = \craft\elements\User::find()
    ->dateCreated(['and', ">= {$start}", "< {$end}"])
    ->all();

:::

dateUpdated

Defined by craft\elements\db\ElementQuery

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

Possible values include:

| Value | Fetches users… | - | - | '>= 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. | now/today/tomorrow/yesterday | that were updated at midnight of the specified relative date.

::: code

{# Fetch users updated in the last week #}
{% set lastWeek = date('1 week ago')|atom %}

{% set users = craft.users()
  .dateUpdated(">= #{lastWeek}")
  .all() %}
// Fetch users updated in the last week
$lastWeek = (new \DateTime('1 week ago'))->format(\DateTime::ATOM);

$users = \craft\elements\User::find()
    ->dateUpdated(">= {$lastWeek}")
    ->all();

:::

eagerly

Defined by craft\elements\db\ElementQuery

Causes the query to be used to eager-load results for the query’s source element and any other elements in its collection.

email

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

Possible values include:

| Value | Fetches users… | - | - | 'me@domain.tld' | with an email of me@domain.tld. | 'not me@domain.tld' | not with an email of me@domain.tld. | '*@domain.tld' | with an email that ends with @domain.tld.

::: code

{# Fetch users with a .co.uk domain on their email address #}
{% set users = craft.users()
  .email('*.co.uk')
  .all() %}
// Fetch users with a .co.uk domain on their email address
$users = \craft\elements\User::find()
    ->email('*.co.uk')
    ->all();

:::

firstName

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

Possible values include:

| Value | Fetches users… | - | - | 'Jane' | with a first name of Jane. | 'not Jane' | not with a first name of Jane.

::: code

{# Fetch all the Jane's #}
{% set users = craft.users()
  .firstName('Jane')
  .all() %}
// Fetch all the Jane's
$users = \craft\elements\User::find()
    ->firstName('Jane')
    ->one();

:::

fixedOrder

Defined by craft\elements\db\ElementQuery

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

::: tip If no IDs were passed to id, setting this to true will result in an empty result set. :::

::: code

{# Fetch users in a specific order #}
{% set users = craft.users()
  .id([1, 2, 3, 4, 5])
  .fixedOrder()
  .all() %}
// Fetch users in a specific order
$users = \craft\elements\User::find()
    ->id([1, 2, 3, 4, 5])
    ->fixedOrder()
    ->all();

:::

fullName

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

Possible values include:

| Value | Fetches users… | - | - | 'Jane Doe' | with a full name of Jane Doe. | 'not Jane Doe' | not with a full name of Jane Doe.

::: code

{# Fetch all the Jane Doe's #}
{% set users = craft.users()
  .fullName('Jane Doe')
  .all() %}
// Fetch all the Jane Doe's
$users = \craft\elements\User::find()
    ->fullName('JaneDoe')
    ->one();

:::

getFieldLayouts

Defined by craft\elements\db\ElementQuery

Returns the field layouts that could be associated with the resulting elements.

group

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

Possible values include:

| Value | Fetches users… | - | - | '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. | ['and', 'foo', 'bar'] | in both groups with handles 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 users in the Foo user group #}
{% set users = craft.users()
  .group('foo')
  .all() %}
// Fetch users in the Foo user group
$users = \craft\elements\User::find()
    ->group('foo')
    ->all();

:::

groupId

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

Possible values include:

| Value | Fetches users… | - | - | 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. | ['and', 1, 2] | in both groups with IDs of 1 or 2. | ['not', 1, 2] | not in a group with an ID of 1 or 2.

::: code

{# Fetch users in a group with an ID of 1 #}
{% set users = craft.users()
  .groupId(1)
  .all() %}
// Fetch users in a group with an ID of 1
$users = \craft\elements\User::find()
    ->groupId(1)
    ->all();

:::

hasPhoto

Narrows the query results to only users that have (or don’t have) a user photo.

::: code

{# Fetch users with photos #}
{% set users = craft.users()
  .hasPhoto()
  .all() %}
// Fetch users without photos
$users = \craft\elements\User::find()
    ->hasPhoto()
    ->all();

:::

id

Defined by craft\elements\db\ElementQuery

Narrows the query results based on the users’ IDs.

Possible values include:

| Value | Fetches users… | - | - | 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 user by its ID #}
{% set user = craft.users()
  .id(1)
  .one() %}
// Fetch the user by its ID
$user = \craft\elements\User::find()
    ->id(1)
    ->one();

:::

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

ignorePlaceholders

Defined by craft\elements\db\ElementQuery

Causes the query to return matching users as they are stored in the database, ignoring matching placeholder elements that were set by craft\services\Elements::setPlaceholderElement().

inBulkOp

Defined by craft\elements\db\ElementQuery

Narrows the query results to only users that were involved in a bulk element operation.

inReverse

Defined by craft\elements\db\ElementQuery

Causes the query results to be returned in reverse order.

::: code

{# Fetch users in reverse #}
{% set users = craft.users()
  .inReverse()
  .all() %}
// Fetch users in reverse
$users = \craft\elements\User::find()
    ->inReverse()
    ->all();

:::

language

Defined by craft\elements\db\ElementQuery

Determines which site(s) the users should be queried in, based on their language.

Possible values include:

| Value | Fetches users… | - | - | 'en' | from sites with a language of en. | ['en-GB', 'en-US'] | from sites with a language of en-GB or en-US. | ['not', 'en-GB', 'en-US'] | not in sites with a language of en-GB or en-US.

::: tip Elements that belong to multiple sites will be returned multiple times by default. If you only want unique elements to be returned, use unique() in conjunction with this. :::

::: code

{# Fetch users from English sites #}
{% set users = craft.users()
  .language('en')
  .all() %}
// Fetch users from English sites
$users = \craft\elements\User::find()
    ->language('en')
    ->all();

:::

lastLoginDate

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

Possible values include:

| Value | Fetches users… | - | - | '>= 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. | now/today/tomorrow/yesterday | that last logged in at midnight of the specified relative date.

::: code

{# Fetch users that logged in recently #}
{% set aWeekAgo = date('7 days ago')|atom %}

{% set users = craft.users()
  .lastLoginDate(">= #{aWeekAgo}")
  .all() %}
// Fetch users that logged in recently
$aWeekAgo = (new \DateTime('7 days ago'))->format(\DateTime::ATOM);

$users = \craft\elements\User::find()
    ->lastLoginDate(">= {$aWeekAgo}")
    ->all();

:::

lastName

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

Possible values include:

| Value | Fetches users… | - | - | 'Doe' | with a last name of Doe. | 'not Doe' | not with a last name of Doe.

::: code

{# Fetch all the Doe's #}
{% set users = craft.users()
  .lastName('Doe')
  .all() %}
// Fetch all the Doe's
$users = \craft\elements\User::find()
    ->lastName('Doe')
    ->one();

:::

limit

Defined by yii\db\QueryTrait

Determines the number of users that should be returned.

::: code

{# Fetch up to 10 users  #}
{% set users = craft.users()
  .limit(10)
  .all() %}
// Fetch up to 10 users
$users = \craft\elements\User::find()
    ->limit(10)
    ->all();

:::

notRelatedTo

Defined by craft\elements\db\ElementQuery

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

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

::: code

{# Fetch all users that are related to myEntry #}
{% set users = craft.users()
  .notRelatedTo(myEntry)
  .all() %}
// Fetch all users that are related to $myEntry
$users = \craft\elements\User::find()
    ->notRelatedTo($myEntry)
    ->all();

:::

offset

Defined by yii\db\QueryTrait

Determines how many users should be skipped in the results.

::: code

{# Fetch all users except for the first 3 #}
{% set users = craft.users()
  .offset(3)
  .all() %}
// Fetch all users except for the first 3
$users = \craft\elements\User::find()
    ->offset(3)
    ->all();

:::

orderBy

Defined by craft\elements\db\ElementQuery

Determines the order that the users should be returned in. (If empty, defaults to username ASC.)

::: code

{# Fetch all users in order of date created #}
{% set users = craft.users()
  .orderBy('dateCreated ASC')
  .all() %}
// Fetch all users in order of date created
$users = \craft\elements\User::find()
    ->orderBy('dateCreated ASC')
    ->all();

:::

preferSites

Defined by craft\elements\db\ElementQuery

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 B, and Bar will be returned for Site C.

If this isn’t set, then preference goes to the current site.

::: code

{# Fetch unique users from Site A, or Site B if they don’t exist in Site A #}
{% set users = craft.users()
  .site('*')
  .unique()
  .preferSites(['a', 'b'])
  .all() %}
// Fetch unique users from Site A, or Site B if they don’t exist in Site A
$users = \craft\elements\User::find()
    ->site('*')
    ->unique()
    ->preferSites(['a', 'b'])
    ->all();

:::

prepForEagerLoading

Defined by craft\elements\db\ElementQuery

Prepares the query for lazy eager loading.

prepareSubquery

Defined by craft\elements\db\ElementQuery

Prepares the element query and returns its subquery (which determines what elements will be returned).

relatedTo

Defined by craft\elements\db\ElementQuery

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

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

::: code

{# Fetch all users that are related to myCategory #}
{% set users = craft.users()
  .relatedTo(myCategory)
  .all() %}
// Fetch all users that are related to $myCategory
$users = \craft\elements\User::find()
    ->relatedTo($myCategory)
    ->all();

:::

render

Defined by craft\elements\db\ElementQuery

Executes the query and renders the resulting elements using their partial templates.

If no partial template exists for an element, its string representation will be output instead.

search

Defined by craft\elements\db\ElementQuery

Narrows the query results to only users 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 users that match the search query #}
{% set users = craft.users()
  .search(searchQuery)
  .all() %}
// Get the search query from the 'q' query string param
$searchQuery = \Craft::$app->request->getQueryParam('q');

// Fetch all users that match the search query
$users = \craft\elements\User::find()
    ->search($searchQuery)
    ->all();

:::

siteSettingsId

Defined by craft\elements\db\ElementQuery

Narrows the query results based on the users’ IDs in the elements_sites table.

Possible values include:

| Value | Fetches users… | - | - | 1 | with an elements_sites ID of 1. | 'not 1' | not with an elements_sites ID of 1. | [1, 2] | with an elements_sites ID of 1 or 2. | ['not', 1, 2] | not with an elements_sites ID of 1 or 2.

::: code

{# Fetch the user by its ID in the elements_sites table #}
{% set user = craft.users()
  .siteSettingsId(1)
  .one() %}
// Fetch the user by its ID in the elements_sites table
$user = \craft\elements\User::find()
    ->siteSettingsId(1)
    ->one();

:::

status

Narrows the query results based on the users’ statuses.

Possible values include:

| Value | Fetches users… | - | - | 'inactive' | with inactive accounts. | 'active' | with active accounts. | 'pending' | with accounts that are still pending activation. | 'credentialed' | with either active or pending accounts. | 'suspended' | with suspended accounts. | 'locked' | with locked accounts (regardless of whether they’re active or suspended). | ['active', 'suspended'] | with active or suspended accounts. | ['not', 'active', 'suspended'] | without active or suspended accounts.

::: code

{# Fetch active and locked users #}
{% set users = craft.users()
  .status(['active', 'locked'])
  .all() %}
// Fetch active and locked users
$users = \craft\elements\User::find()
    ->status(['active', 'locked'])
    ->all();

:::

trashed

Defined by craft\elements\db\ElementQuery

Narrows the query results to only users that have been soft-deleted.

::: code

{# Fetch trashed users #}
{% set users = craft.users()
  .trashed()
  .all() %}
// Fetch trashed users
$users = \craft\elements\User::find()
    ->trashed()
    ->all();

:::

uid

Defined by craft\elements\db\ElementQuery

Narrows the query results based on the users’ UIDs.

::: code

{# Fetch the user by its UID #}
{% set user = craft.users()
  .uid('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
  .one() %}
// Fetch the user by its UID
$user = \craft\elements\User::find()
    ->uid('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
    ->one();

:::

username

Narrows the query results based on the users’ usernames.

Possible values include:

| Value | Fetches users… | - | - | '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 user = craft.users()
  .username(requestedUsername|literal)
  .one() %}
// Get the requested username
$requestedUsername = \Craft::$app->request->getSegment(2);

// Fetch that user
$user = \craft\elements\User::find()
    ->username(\craft\helpers\Db::escapeParam($requestedUsername))
    ->one();

:::

wasCountEagerLoaded

Defined by craft\elements\db\ElementQuery

Returns whether the query result count was already eager loaded by the query's source element.

wasEagerLoaded

Defined by craft\elements\db\ElementQuery

Returns whether the query results were already eager loaded by the query's source element.

with

Defined by craft\elements\db\ElementQuery

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

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

::: code

{# Fetch users eager-loaded with the "Related" field’s relations #}
{% set users = craft.users()
  .with(['related'])
  .all() %}
// Fetch users eager-loaded with the "Related" field’s relations
$users = \craft\elements\User::find()
    ->with(['related'])
    ->all();

:::

withCustomFields

Defined by craft\elements\db\ElementQuery

Sets whether custom fields should be factored into the query.

withGroups

Causes the query to return matching users eager-loaded with their user groups.

Possible values include:

| Value | Fetches users… | - | - | '>= 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 users with their user groups #}
{% set users = craft.users()
  .withGroups()
  .all() %}
// fetch users with their user groups
$users = \craft\elements\User::find()
    ->withGroups()
    ->all();

:::

withProvisionalDrafts

Defined by craft\elements\db\ElementQuery

Causes the query to return provisional drafts for the matching elements, when they exist for the current user.