ElementQueryInterface

Type
Interface
Namespace
craft\elements\db
Extends
ArrayAccess (opens new window), Countable (opens new window), IteratorAggregate (opens new window), yii\base\Arrayable (opens new window), yii\db\QueryInterface (opens new window)
Implemented by
craft\elements\db\AssetQuery, craft\elements\db\CategoryQuery, craft\elements\db\ElementQuery, craft\elements\db\EntryQuery, craft\elements\db\GlobalSetQuery, craft\elements\db\MatrixBlockQuery, craft\elements\db\TagQuery, craft\elements\db\UserQuery, craft\test\mockclasses\elements\MockElementQuery
Since
3.0.0

ElementQueryInterface defines the common interface to be implemented by element query classes.

The default implementation of this interface is provided by craft\elements\db\ElementQuery.

View source (opens new window)

# Public Methods

Method Description
addOrderBy() (opens new window) Adds additional ORDER BY columns to the query.
afterPopulate() Performs any post-population processing on elements.
all() Executes the query and returns all results as an array.
ancestorDist() Narrows the query results to only {elements} that are up to a certain distance away from the {element} specified by ancestorOf().
ancestorOf() Narrows the query results to only {elements} that are ancestors of another {element} in its structure.
andFilterWhere() (opens new window) Adds an additional WHERE condition to the existing one ignoring empty parameters.
andRelatedTo() Narrows the query results to only {elements} that are related to certain other elements.
andWhere() (opens new window) Adds an additional WHERE condition to the existing one.
andWith() Causes the query to return matching {elements} eager-loaded with related elements, in addition to the elements that were already specified by with().
anyStatus() Removes element filters based on their statuses.
archived() Sets the $archived property.
asArray() Causes the query to return matching {elements} as arrays of data, rather than [[{element-class}]] objects.
count()
createElement() Converts a found row into an element instance.
dateCreated() Narrows the query results based on the {elements}’ creation dates.
dateUpdated() Narrows the query results based on the {elements}’ last-updated dates.
descendantDist() Narrows the query results to only {elements} that are up to a certain distance away from the {element} specified by descendantOf().
descendantOf() Narrows the query results to only {elements} that are descendants of another {element} in its structure.
draftCreator() Narrows the query results to only drafts created by a given user.
draftId() Narrows the query results based on the {elements}’ draft’s ID (from the drafts table).
draftOf() Narrows the query results to only drafts of a given {element}.
drafts() Narrows the query results to only drafts {elements}.
emulateExecution() (opens new window) Sets whether to emulate query execution, preventing any interaction with data storage.
enabledForSite() Narrows the query results based on whether the {elements} are enabled in the site they’re being queried in, per the site() parameter.
exists() (opens new window) Returns a value indicating whether the query result contains any row of data.
extraFields() (opens new window) Returns the list of additional fields that can be returned by toArray() (opens new window) in addition to those listed in fields() (opens new window).
fields() (opens new window) Returns the list of fields that should be returned by default by toArray() (opens new window) when no specific fields are specified.
filterWhere() (opens new window) Sets the WHERE part of the query ignoring empty parameters.
fixedOrder() Causes the query results to be returned in the order specified by id().
hasDescendants() Narrows the query results based on whether the {elements} have any descendants in their structure.
id() Narrows the query results based on the {elements}’ IDs.
ids() Executes the query and returns the IDs of the resulting elements.
ignorePlaceholders() 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().
inReverse() Causes the query results to be returned in reverse order.
indexBy() (opens new window) Sets the indexBy() (opens new window) property.
leaves() Narrows the query results based on whether the {elements} are “leaves” ({elements} with no descendants).
level() Narrows the query results based on the {elements}’ level within the structure.
limit() (opens new window) Sets the LIMIT part of the query.
nextSiblingOf() Narrows the query results to only the {element} that comes immediately after another {element} in its structure.
nth() Executes the query and returns a single row of result at a given offset.
offset() (opens new window) Sets the OFFSET part of the query.
one() Executes the query and returns a single row of result.
orFilterWhere() (opens new window) Adds an additional WHERE condition to the existing one ignoring empty parameters.
orWhere() (opens new window) Adds an additional WHERE condition to the existing one.
orderBy() (opens new window) Sets the ORDER BY part of the query.
positionedAfter() Narrows the query results to only {elements} that are positioned after another {element} in its structure.
positionedBefore() Narrows the query results to only {elements} that are positioned before another {element} in its structure.
preferSites() If unique() is set, this determines which site should be selected when querying multi-site elements.
prevSiblingOf() Narrows the query results to only the {element} that comes immediately before another {element} in its structure.
provisionalDrafts() Narrows the query results to only provisional drafts.
ref() Narrows the query results based on a reference string.
relatedTo() Narrows the query results to only {elements} that are related to certain other elements.
revisionCreator() Narrows the query results to only revisions created by a given user.
revisionId() Narrows the query results based on the {elements}’ revision’s ID (from the revisions table).
revisionOf() Narrows the query results to only revisions of a given {element}.
revisions() Narrows the query results to only revision {elements}.
savedDraftsOnly() Narrows the query results to only unpublished drafts which have been saved after initial creation.
search() Narrows the query results to only {elements} that match a search query.
siblingOf() Narrows the query results to only {elements} that are siblings of another {element} in its structure.
site() Determines which site(s) the {elements} should be queried in.
siteId() Determines which site(s) the {elements} should be queried in, per the site’s ID.
siteSettingsId() Narrows the query results based on the {elements}’ IDs in the elements_sites table.
slug() Narrows the query results based on the {elements}’ slugs.
status() Narrows the query results based on the {elements}’ statuses.
structureId() Determines which structure data should be joined into the query.
title() Narrows the query results based on the {elements}’ titles.
toArray() (opens new window) Converts the object into an array.
trashed() Narrows the query results to only {elements} that have been soft-deleted.
uid() Narrows the query results based on the {elements}’ UIDs.
unique() Determines whether only elements with unique IDs should be returned by the query.
uri() Narrows the query results based on the {elements}’ URIs.
where() (opens new window) Sets the WHERE part of the query.
with() Causes the query to return matching {elements} eager-loaded with related elements.
withStructure() Explicitly determines whether the query should join in the structure data.

# afterPopulate()

Since
3.6.0

Performs any post-population processing on elements.

View source (opens new window)

Arguments

Returns

craft\base\ElementInterface[], array (opens new window)[]

# all()

Executes the query and returns all results as an array.

View source (opens new window)

Arguments

Returns

craft\base\ElementInterface[] – The resulting elements.

# ancestorDist()

Narrows the query results to only {elements} that are up to a certain distance away from the {element} specified by ancestorOf().

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} above this one #}
{% set {elements-var} = {twig-method}
  .ancestorOf({myElement})
  .ancestorDist(3)
  .all() %}

# ancestorOf()

Narrows the query results to only {elements} that are ancestors of another {element} in its structure.

Possible values include:

Value Fetches {elements}…
1 above the {element} with an ID of 1.
a [[{element-class}]] object above the {element} represented by the object.

TIP

This can be combined with ancestorDist() if you want to limit how far away the ancestor {elements} can be.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} above this one #}
{% set {elements-var} = {twig-method}
  .ancestorOf({myElement})
  .all() %}

# andRelatedTo()

Since
3.6.11

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

See Relations (opens new window) for a full explanation of how to work with this parameter.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch all {elements} that are related to myCategoryA and myCategoryB #}
{% set {elements-var} = {twig-method}
  .relatedTo(myCategoryA)
  .andRelatedTo(myCategoryB)
  .all() %}

# andWith()

Since
3.0.9

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

.

View source (opens new window)

Arguments

Returns

self – The query object itself

# anyStatus()

Since
3.0.17

Removes element filters based on their statuses.

View source (opens new window)

Returns

static – Self reference

Example

{# Fetch all {elements}, regardless of status #}
{% set {elements-var} = {twig-method}
  .anyStatus()
  .all() %}

# archived()

Sets the $archived property.

View source (opens new window)

Arguments

Returns

static – Self reference

# asArray()

Causes the query to return matching {elements} as arrays of data, rather than [[{element-class}]] objects.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} as arrays #}
{% set {elements-var} = {twig-method}
  .asArray()
  .all() %}

# count()

View source (opens new window)

Arguments

  • $q
  • $db

# createElement()

Since
3.6.0

Converts a found row into an element instance.

View source (opens new window)

Arguments

Returns

craft\base\ElementInterface

# dateCreated()

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.

View source (opens new window)

Arguments

  • $value (mixed) – The property value

Returns

static – Self reference

Example

{# 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-var} = {twig-method}
  .dateCreated(['and', ">= #{start}", "< #{end}"])
  .all() %}

# dateUpdated()

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.

View source (opens new window)

Arguments

  • $value (mixed) – The property value

Returns

static – Self reference

Example

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

{% set {elements-var} = {twig-method}
  .dateUpdated(">= #{lastWeek}")
  .all() %}

# descendantDist()

Narrows the query results to only {elements} that are up to a certain distance away from the {element} specified by descendantOf().

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} below this one #}
{% set {elements-var} = {twig-method}
  .descendantOf({myElement})
  .descendantDist(3)
  .all() %}

# descendantOf()

Narrows the query results to only {elements} that are descendants of another {element} in its structure.

Possible values include:

Value Fetches {elements}…
1 below the {element} with an ID of 1.
a [[{element-class}]] object below the {element} represented by the object.

TIP

This can be combined with descendantDist() if you want to limit how far away the descendant {elements} can be.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} below this one #}
{% set {elements-var} = {twig-method}
  .descendantOf({myElement})
  .all() %}

# draftCreator()

Since
3.2.0

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\db\User object created by the user represented by the object.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch drafts by the current user #}
{% set {elements-var} = {twig-method}
  .draftCreator(currentUser)
  .all() %}

# draftId()

Since
3.2.0

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.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch a draft #}
{% set {elements-var} = {twig-method}
  .draftId(10)
  .all() %}

# draftOf()

Since
3.2.0

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 [[{element-class}]] object for the {element} represented by the object.
'*' for any {element}
false that aren’t associated with a published {element}

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch drafts of the {element} #}
{% set {elements-var} = {twig-method}
  .draftOf({myElement})
  .all() %}

# drafts()

Since
3.2.0

Narrows the query results to only drafts {elements}.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch a draft {element} #}
{% set {elements-var} = {twig-method}
  .drafts()
  .id(123)
  .one() %}

# enabledForSite()

DEPRECATED

Deprecated in 3.5.0. status() should be used instead.

Narrows the query results based on whether the {elements} are enabled in the site they’re being queried in, per the site() parameter.

Possible values include:

Value Fetches {elements}…
true (default) that are enabled in the site.
false whether they are enabled or not in the site.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch all {elements}, including ones disabled for this site #}
{% set {elements-var} = {twig-method}
  .enabledForSite(false)
  .all() %}

# fixedOrder()

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.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

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

# hasDescendants()

Since
3.0.4

Narrows the query results based on whether the {elements} have any descendants in their structure.

(This has the opposite effect of calling leaves().)

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} that have descendants #}
{% set {elements-var} = {twig-method}
  .hasDescendants()
  .all() %}

# id()

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.

TIP

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

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch the {element} by its ID #}
{% set {element-var} = {twig-method}
  .id(1)
  .one() %}

# ids()

Executes the query and returns the IDs of the resulting elements.

View source (opens new window)

Arguments

Returns

integer (opens new window)[] – The resulting element IDs. An empty array is returned if no elements are found.

# ignorePlaceholders()

Since
3.2.9

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().

View source (opens new window)

Arguments

Returns

static – Self reference

# inReverse()

Causes the query results to be returned in reverse order.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} in reverse #}
{% set {elements-var} = {twig-method}
  .inReverse()
  .all() %}

# leaves()

Narrows the query results based on whether the {elements} are “leaves” ({elements} with no descendants).

(This has the opposite effect of calling hasDescendants().)

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} that have no descendants #}
{% set {elements-var} = {twig-method}
  .leaves()
  .all() %}

# level()

Narrows the query results based on the {elements}’ level within the structure.

Possible values include:

Value Fetches {elements}…
1 with a level of 1.
'not 1' not with a level of 1.
'>= 3' with a level greater than or equal to 3.
[1, 2] with a level of 1 or 2
['not', 1, 2] not with level of 1 or 2.

View source (opens new window)

Arguments

  • $value (mixed) – The property value

Returns

static – Self reference

Example

{# Fetch {elements} positioned at level 3 or above #}
{% set {elements-var} = {twig-method}
  .level('>= 3')
  .all() %}

# nextSiblingOf()

Narrows the query results to only the {element} that comes immediately after another {element} in its structure.

Possible values include:

Value Fetches the {element}…
1 after the {element} with an ID of 1.
a [[{element-class}]] object after the {element} represented by the object.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch the next {element} #}
{% set {element-var} = {twig-method}
  .nextSiblingOf({myElement})
  .one() %}

# nth()

Executes the query and returns a single row of result at a given offset.

View source (opens new window)

Arguments

Returns

craft\base\ElementInterface, array (opens new window), null (opens new window) – The element or row of the query result. Null is returned if the query results in nothing.

# one()

Executes the query and returns a single row of result.

View source (opens new window)

Arguments

Returns

craft\base\ElementInterface, array (opens new window), null (opens new window) – The resulting element. Null is returned if the query results in nothing.

# positionedAfter()

Narrows the query results to only {elements} that are positioned after another {element} in its structure.

Possible values include:

Value Fetches {elements}…
1 after the {element} with an ID of 1.
a [[{element-class}]] object after the {element} represented by the object.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} after this one #}
{% set {elements-var} = {twig-method}
  .positionedAfter({myElement})
  .all() %}

# positionedBefore()

Narrows the query results to only {elements} that are positioned before another {element} in its structure.

Possible values include:

Value Fetches {elements}…
1 before the {element} with an ID of 1.
a [[{element-class}]] object before the {element} represented by the object.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} before this one #}
{% set {elements-var} = {twig-method}
  .positionedBefore({myElement})
  .all() %}

# preferSites()

Since
3.2.0

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.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch unique {elements} from Site A, or Site B if they don’t exist in Site A #}
{% set {elements-var} = {twig-method}
  .site('*')
  .unique()
  .preferSites(['a', 'b'])
  .all() %}

# prevSiblingOf()

Narrows the query results to only the {element} that comes immediately before another {element} in its structure.

Possible values include:

Value Fetches the {element}…
1 before the {element} with an ID of 1.
a [[{element-class}]] object before the {element} represented by the object.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch the previous {element} #}
{% set {element-var} = {twig-method}
  .prevSiblingOf({myElement})
  .one() %}

# provisionalDrafts()

Since
3.7.0

Narrows the query results to only provisional drafts.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch provisional drafts created by the current user #}
{% set {elements-var} = {twig-method}
  .provisionalDrafts()
  .draftCreator(currentUser)
  .all() %}

# ref()

Narrows the query results based on a reference string.

View source (opens new window)

Arguments

Returns

static – Self reference

# relatedTo()

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

See Relations (opens new window) for a full explanation of how to work with this parameter.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch all {elements} that are related to myCategory #}
{% set {elements-var} = {twig-method}
  .relatedTo(myCategory)
  .all() %}

# revisionCreator()

Since
3.2.0

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\db\User object created by the user represented by the object.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch revisions by the current user #}
{% set {elements-var} = {twig-method}
  .revisionCreator(currentUser)
  .all() %}

# revisionId()

Since
3.2.0

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.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch a revision #}
{% set {elements-var} = {twig-method}
  .revisionId(10)
  .all() %}

# revisionOf()

Since
3.2.0

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 [[{element-class}]] object for the {element} represented by the object.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch revisions of the {element} #}
{% set {elements-var} = {twig-method}
  .revisionOf({myElement})
  .all() %}

# revisions()

Since
3.2.0

Narrows the query results to only revision {elements}.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch a revision {element} #}
{% set {elements-var} = {twig-method}
  .revisions()
  .id(123)
  .one() %}

# savedDraftsOnly()

Since
3.6.6

Narrows the query results to only unpublished drafts which have been saved after initial creation.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch saved, unpublished draft {elements} #}
{% set {elements-var} = {twig-method}
  .draftOf(false)
  .savedDraftsOnly()
  .all() %}

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

See Searching (opens new window) for a full explanation of how to work with this parameter.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# 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-var} = {twig-method}
  .search(searchQuery)
  .all() %}

# siblingOf()

Narrows the query results to only {elements} that are siblings of another {element} in its structure.

Possible values include:

Value Fetches {elements}…
1 beside the {element} with an ID of 1.
a [[{element-class}]] object beside the {element} represented by the object.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} beside this one #}
{% set {elements-var} = {twig-method}
  .siblingOf({myElement})
  .all() %}

# site()

Determines which site(s) the {elements} should be queried in.

The current site will be used by default.

Possible values include:

Value Fetches {elements}…
'foo' from the site with a handle of foo.
['foo', 'bar'] from a site with a handle of foo or bar.
['not', 'foo', 'bar'] not in a site with a handle of foo or bar.
a craft\models\Site object from the site represented by the object.
'*' from any site.

TIP

If multiple sites are specified, elements that belong to multiple sites will be returned multiple times. If you only want unique elements to be returned, use unique() in conjunction with this.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} from the Foo site #}
{% set {elements-var} = {twig-method}
  .site('foo')
  .all() %}

# siteId()

Determines which site(s) the {elements} should be queried in, per the site’s ID.

The current site will be used by default.

Possible values include:

Value Fetches {elements}…
1 from the site with an ID of 1.
[1, 2] from a site with an ID of 1 or 2.
['not', 1, 2] not in a site with an ID of 1 or 2.
'*' from any site.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} from the site with an ID of 1 #}
{% set {elements-var} = {twig-method}
  .siteId(1)
  .all() %}

# siteSettingsId()

Since
3.7.0

Narrows the query results based on the {elements}’ IDs in the elements_sites table.

Possible values include:

Value Fetches {elements}…
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.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch the {element} by its ID in the elements_sites table #}
{% set {element-var} = {twig-method}
  .siteSettingsId(1)
  .one() %}

# slug()

Narrows the query results based on the {elements}’ slugs.

Possible values include:

Value Fetches {elements}…
'foo' with a slug of foo.
'foo*' with a slug that begins with foo.
'*foo' with a slug that ends with foo.
'*foo*' with a slug that contains foo.
'not *foo*' with a slug that doesn’t contain foo.
['*foo*', '*bar*'] with a slug that contains foo or bar.
['not', '*foo*', '*bar*'] with a slug that doesn’t contain foo or bar.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Get the requested {element} slug from the URL #}
{% set requestedSlug = craft.app.request.getSegment(3) %}

{# Fetch the {element} with that slug #}
{% set {element-var} = {twig-method}
  .slug(requestedSlug|literal)
  .one() %}

# status()

Narrows the query results based on the {elements}’ statuses.

Possible values include:

Value Fetches {elements}…
'enabled' (default) that are enabled.
'disabled' that are disabled.
['not', 'disabled'] that are not disabled.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch disabled {elements} #}
{% set {elements-var} = {twig-method}
  .status('disabled')
  .all() %}

# structureId()

Determines which structure data should be joined into the query.

View source (opens new window)

Arguments

Returns

static – Self reference

# title()

Narrows the query results based on the {elements}’ titles.

Possible values include:

Value Fetches {elements}…
'Foo' with a title of Foo.
'Foo*' with a title that begins with Foo.
'*Foo' with a title that ends with Foo.
'*Foo*' with a title that contains Foo.
'not *Foo*' with a title that doesn’t contain Foo.
['*Foo*', '*Bar*'] with a title that contains Foo or Bar.
['not', '*Foo*', '*Bar*'] with a title that doesn’t contain Foo or Bar.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch {elements} with a title that contains "Foo" #}
{% set {elements-var} = {twig-method}
  .title('*Foo*')
  .all() %}

:::

# trashed()

Since
3.1.0

Narrows the query results to only {elements} that have been soft-deleted.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch trashed {elements} #}
{% set {elements-var} = {twig-method}
  .trashed()
  .all() %}

# uid()

Narrows the query results based on the {elements}’ UIDs.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

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

# unique()

Since
3.2.0

Determines whether only elements with unique IDs should be returned by the query.

This should be used when querying elements from multiple sites at the same time, if “duplicate” results is not desired.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Fetch unique {elements} across all sites #}
{% set {elements-var} = {twig-method}
  .site('*')
  .unique()
  .all() %}

# uri()

Narrows the query results based on the {elements}’ URIs.

Possible values include:

Value Fetches {elements}…
'foo' with a URI of foo.
'foo*' with a URI that begins with foo.
'*foo' with a URI that ends with foo.
'*foo*' with a URI that contains foo.
'not *foo*' with a URI that doesn’t contain foo.
['*foo*', '*bar*'] with a URI that contains foo or bar.
['not', '*foo*', '*bar*'] with a URI that doesn’t contain foo or bar.

View source (opens new window)

Arguments

Returns

static – Self reference

Example

{# Get the requested URI #}
{% set requestedUri = craft.app.request.getPathInfo() %}

{# Fetch the {element} with that URI #}
{% set {element-var} = {twig-method}
  .uri(requestedUri|literal)
  .one() %}

# with()

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

See Eager-Loading Elements (opens new window) for a full explanation of how to work with this parameter.

View source (opens new window)

Arguments

Returns

self – The query object itself

Example

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

# withStructure()

Explicitly determines whether the query should join in the structure data.

View source (opens new window)

Arguments

Returns

static – Self reference