Skip to content

Latest commit

 

History

History
1811 lines (1214 loc) · 34.1 KB

File metadata and controls

1811 lines (1214 loc) · 34.1 KB

Entry Queries

You can fetch entries in your templates or PHP code using entry queries.

::: code

{# Create a new entry query #}
{% set myEntryQuery = craft.entries() %}
// Create a new entry query
$myEntryQuery = \craft\elements\Entry::find();

:::

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

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

Example

We can display the 10 most recent entries in a “Blog” section by doing the following:

  1. Create an entry query with craft.entries().
  2. Set the section and limit parameters on it.
  3. Fetch the entries with .all().
  4. Loop through the entries using a for tag to output the blog post HTML.
{# Create an entry query with the 'section' and 'limit' parameters #}
{% set myEntryQuery = craft.entries()
    .section('blog')
    .limit(10) %}

{# Fetch the entries #}
{% set entries = myEntryQuery.all() %}

{# Display the entries #}
{% for entry in entries %}
    <article>
        <h1><a href="{{ entry.url }}">{{ entry.title }}</a></h1>
        {{ entry.summary }}
        <a href="{{ entry.url }}">Continue reading</a>
    </article>
{% endfor %}

Parameters

Entry queries support the following parameters:

after

Narrows the query results to only entries that were posted on or after a certain date.

Possible values include:

Value Fetches entries…
'2018-04-01' that were posted after 2018-04-01.
a DateTime object that were posted after the date represented by the object.

::: code

{# Fetch entries posted this month #}
{% set firstDayOfMonth = date('first day of this month') %}

{% set entries = craft.entries()
    .after(firstDayOfMonth)
    .all() %}
// Fetch entries posted this month
$firstDayOfMonth = new \DateTime('first day of this month');

$entries = \craft\elements\Entry::find()
    ->after($firstDayOfMonth)
    ->all();

:::

ancestorDist

Narrows the query results to only entries that are up to a certain distance away from the entry specified by ancestorOf.

::: code

{# Fetch entries above this one #}
{% set entries = craft.entries()
    .ancestorOf(myEntry)
    .ancestorDist(3)
    .all() %}
// Fetch entries above this one
$entries = \craft\elements\Entry::find()
    ->ancestorOf($myEntry)
    ->ancestorDist(3)
    ->all();

:::

ancestorOf

Narrows the query results to only entries that are ancestors of another entry.

Possible values include:

Value Fetches entries…
1 above the entry with an ID of 1.
a Entry object above the entry represented by the object.

::: code

{# Fetch entries above this one #}
{% set entries = craft.entries()
    .ancestorOf(myEntry)
    .all() %}
// Fetch entries above this one
$entries = \craft\elements\Entry::find()
    ->ancestorOf($myEntry)
    ->all();

:::

::: tip This can be combined with ancestorDist if you want to limit how far away the ancestor entries can be. :::

anyStatus

Clears out the status and enabledForSite parameters.

::: code

{# Fetch all entries, regardless of status #}
{% set entries = craft.entries()
    .anyStatus()
    .all() %}
// Fetch all entries, regardless of status
$entries = \craft\elements\Entry::find()
    ->anyStatus()
    ->all();

:::

asArray

Causes the query to return matching entries as arrays of data, rather than Entry objects.

::: code

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

:::

authorGroup

Narrows the query results based on the user group the entries’ authors belong to.

Possible values include:

Value Fetches entries…
'foo' with an author in a group with a handle of foo.
'not foo' not with an author in a group with a handle of foo.
['foo', 'bar'] with an author in a group with a handle of foo or bar.
['not', 'foo', 'bar'] not with an author in a group with a handle of foo or bar.
a UserGroup object with an author in a group represented by the object.

::: code

{# Fetch entries with an author in the Foo user group #}
{% set entries = craft.entries()
    .authorGroup('foo')
    .all() %}
// Fetch entries with an author in the Foo user group
$entries = \craft\elements\Entry::find()
    ->authorGroup('foo')
    ->all();

:::

authorGroupId

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

Possible values include:

Value Fetches entries…
1 with an author in a group with an ID of 1.
'not 1' not with an author in a group with an ID of 1.
[1, 2] with an author in a group with an ID of 1 or 2.
['not', 1, 2] not with an author in a group with an ID of 1 or 2.

::: code

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

:::

authorId

Narrows the query results based on the entries’ authors.

Possible values include:

Value Fetches entries…
1 with an author with an ID of 1.
'not 1' not with an author with an ID of 1.
[1, 2] with an author with an ID of 1 or 2.
['not', 1, 2] not with an author with an ID of 1 or 2.

::: code

{# Fetch entries with an author with an ID of 1 #}
{% set entries = craft.entries()
    .authorId(1)
    .all() %}
// Fetch entries with an author with an ID of 1
$entries = \craft\elements\Entry::find()
    ->authorId(1)
    ->all();

:::

before

Narrows the query results to only entries that were posted before a certain date.

Possible values include:

Value Fetches entries…
'2018-04-01' that were posted before 2018-04-01.
a DateTime object that were posted before the date represented by the object.

::: code

{# Fetch entries posted before this month #}
{% set firstDayOfMonth = date('first day of this month') %}

{% set entries = craft.entries()
    .before(firstDayOfMonth)
    .all() %}
// Fetch entries posted before this month
$firstDayOfMonth = new \DateTime('first day of this month');

$entries = \craft\elements\Entry::find()
    ->before($firstDayOfMonth)
    ->all();

:::

dateCreated

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

Possible values include:

Value Fetches entries…
'>= 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 entries created last month #}
{% set start = date('first day of last month')|atom %}
{% set end = date('first day of this month')|atom %}

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

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

:::

dateUpdated

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

Possible values include:

Value Fetches entries…
'>= 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 entries updated in the last week #}
{% set lastWeek = date('1 week ago')|atom %}

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

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

:::

descendantDist

Narrows the query results to only entries that are up to a certain distance away from the entry specified by descendantOf.

::: code

{# Fetch entries below this one #}
{% set entries = craft.entries()
    .descendantOf(myEntry)
    .descendantDist(3)
    .all() %}
// Fetch entries below this one
$entries = \craft\elements\Entry::find()
    ->descendantOf($myEntry)
    ->descendantDist(3)
    ->all();

:::

descendantOf

Narrows the query results to only entries that are descendants of another entry.

Possible values include:

Value Fetches entries…
1 below the entry with an ID of 1.
a Entry object below the entry represented by the object.

::: code

{# Fetch entries below this one #}
{% set entries = craft.entries()
    .descendantOf(myEntry)
    .all() %}
// Fetch entries below this one
$entries = \craft\elements\Entry::find()
    ->descendantOf($myEntry)
    ->all();

:::

::: tip This can be combined with descendantDist if you want to limit how far away the descendant entries can be. :::

draftCreator

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 by the user represented by the object.

::: code

{# Fetch drafts by the current user #}
{% set entries = craft.entries()
    .draftCreator(currentUser)
    .all() %}
// Fetch drafts by the current user
$entries = \craft\elements\Entry::find()
    ->draftCreator(Craft::$app->user->identity)
    ->all();

:::

draftId

Narrows the query results based on the entries’ 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 entries = craft.entries()
    .draftId(10)
    .all() %}
// Fetch a draft
$entries = \craft\elements\Entry::find()
    ->draftIf(10)
    ->all();

:::

draftOf

Narrows the query results to only drafts of a given entry.

Possible values include:

Value Fetches drafts…
1 for the entry with an ID of 1.
a Entry object for the entry represented by the object.

::: code

{# Fetch drafts of the entry #}
{% set entries = craft.entries()
    .draftOf(myEntry)
    .all() %}
// Fetch drafts of the entry
$entries = \craft\elements\Entry::find()
    ->draftOf($myEntry)
    ->all();

:::

drafts

Narrows the query results to only drafts entries.

::: code

{# Fetch a draft entry #}
{% set entries = {twig-function}
    .drafts()
    .id(123)
    .one() %}
// Fetch a draft entry
$entries = \craft\elements\Entry::find()
    ->drafts()
    ->id(123)
    ->one();

:::

enabledForSite

Narrows the query results based on whether the entries are enabled in the site they’re being queried in, per the site parameter.

Possible values include:

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

::: code

{# Fetch all entries, including ones disabled for this site #}
{% set entries = craft.entries()
    .enabledForSite(false)
    .all() %}
// Fetch all entries, including ones disabled for this site
$entries = \craft\elements\Entry::find()
    ->enabledForSite(false)
    ->all();

:::

expiryDate

Narrows the query results based on the entries’ expiry dates.

Possible values include:

Value Fetches entries…
':empty:' that don’t have an expiry date.
':notempty:' that have an expiry date.
'>= 2020-04-01' that will expire on or after 2020-04-01.
'< 2020-05-01' that will expire before 2020-05-01
['and', '>= 2020-04-04', '< 2020-05-01'] that will expire between 2020-04-01 and 2020-05-01.

::: code

{# Fetch entries expiring this month #}
{% set nextMonth = date('first day of next month')|atom %}

{% set entries = craft.entries()
    .expiryDate("< #{nextMonth}")
    .all() %}
// Fetch entries expiring this month
$nextMonth = (new \DateTime('first day of next month'))->format(\DateTime::ATOM);

$entries = \craft\elements\Entry::find()
    ->expiryDate("< {$nextMonth}")
    ->all();

:::

fixedOrder

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

::: code

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

:::

hasDescendants

Narrows the query results based on whether the entries have any descendants.

(This has the opposite effect of calling leaves.)

::: code

{# Fetch entries that have descendants #}
{% set entries = craft.entries()
    .hasDescendants()
    .all() %}
// Fetch entries that have descendants
$entries = \craft\elements\Entry::find()
    ->hasDescendants()
    ->all();

:::

id

Narrows the query results based on the entries’ IDs.

Possible values include:

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

:::

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

ignorePlaceholders

Causes the query to return matching entries 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.

::: code

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

:::

leaves

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

(This has the opposite effect of calling hasDescendants.)

::: code

{# Fetch entries that have no descendants #}
{% set entries = craft.entries()
    .leaves()
    .all() %}
// Fetch entries that have no descendants
$entries = \craft\elements\Entry::find()
    ->leaves()
    ->all();

:::

level

Narrows the query results based on the entries’ level within the structure.

Possible values include:

Value Fetches entries…
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.

::: code

{# Fetch entries positioned at level 3 or above #}
{% set entries = craft.entries()
    .level('>= 3')
    .all() %}
// Fetch entries positioned at level 3 or above
$entries = \craft\elements\Entry::find()
    ->level('>= 3')
    ->all();

:::

limit

Determines the number of entries that should be returned.

::: code

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

:::

nextSiblingOf

Narrows the query results to only the entry that comes immediately after another entry.

Possible values include:

Value Fetches the entry…
1 after the entry with an ID of 1.
a Entry object after the entry represented by the object.

::: code

{# Fetch the next entry #}
{% set entry = craft.entries()
    .nextSiblingOf(myEntry)
    .one() %}
// Fetch the next entry
$entry = \craft\elements\Entry::find()
    ->nextSiblingOf($myEntry)
    ->one();

:::

offset

Determines how many entries should be skipped in the results.

::: code

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

:::

orderBy

Determines the order that the entries should be returned in.

::: code

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

:::

positionedAfter

Narrows the query results to only entries that are positioned after another entry.

Possible values include:

Value Fetches entries…
1 after the entry with an ID of 1.
a Entry object after the entry represented by the object.

::: code

{# Fetch entries after this one #}
{% set entries = craft.entries()
    .positionedAfter(myEntry)
    .all() %}
// Fetch entries after this one
$entries = \craft\elements\Entry::find()
    ->positionedAfter($myEntry)
    ->all();

:::

positionedBefore

Narrows the query results to only entries that are positioned before another entry.

Possible values include:

Value Fetches entries…
1 before the entry with an ID of 1.
a Entry object before the entry represented by the object.

::: code

{# Fetch entries before this one #}
{% set entries = craft.entries()
    .positionedBefore(myEntry)
    .all() %}
// Fetch entries before this one
$entries = \craft\elements\Entry::find()
    ->positionedBefore($myEntry)
    ->all();

:::

postDate

Narrows the query results based on the entries’ post dates.

Possible values include:

Value Fetches entries…
'>= 2018-04-01' that were posted on or after 2018-04-01.
'< 2018-05-01' that were posted before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01'] that were posted between 2018-04-01 and 2018-05-01.

::: code

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

{% set entries = craft.entries()
    .postDate(['and', ">= #{start}", "< #{end}"])
    .all() %}
// Fetch entries posted last month
$start = (new \DateTime('first day of last month'))->format(\DateTime::ATOM);
$end = (new \DateTime('first day of this month'))->format(\DateTime::ATOM);

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

:::

preferSites

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

:::

prevSiblingOf

Narrows the query results to only the entry that comes immediately before another entry.

Possible values include:

Value Fetches the entry…
1 before the entry with an ID of 1.
a Entry object before the entry represented by the object.

::: code

{# Fetch the previous entry #}
{% set entry = craft.entries()
    .prevSiblingOf(myEntry)
    .one() %}
// Fetch the previous entry
$entry = \craft\elements\Entry::find()
    ->prevSiblingOf($myEntry)
    ->one();

:::

relatedTo

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

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

::: code

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

:::

revisionCreator

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 by the user represented by the object.

::: code

{# Fetch revisions by the current user #}
{% set entries = craft.entries()
    .revisionCreator(currentUser)
    .all() %}
// Fetch revisions by the current user
$entries = \craft\elements\Entry::find()
    ->revisionCreator(Craft::$app->user->identity)
    ->all();

:::

revisionId

Narrows the query results based on the entries’ 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 entries = craft.entries()
    .revisionId(10)
    .all() %}
// Fetch a revision
$entries = \craft\elements\Entry::find()
    ->revisionIf(10)
    ->all();

:::

revisionOf

Narrows the query results to only revisions of a given entry.

Possible values include:

Value Fetches revisions…
1 for the entry with an ID of 1.
a Entry object for the entry represented by the object.

::: code

{# Fetch revisions of the entry #}
{% set entries = craft.entries()
    .revisionOf(myEntry)
    .all() %}
// Fetch revisions of the entry
$entries = \craft\elements\Entry::find()
    ->revisionOf($myEntry)
    ->all();

:::

revisions

Narrows the query results to only revision entries.

::: code

{# Fetch a revision entry #}
{% set entries = {twig-function}
    .revisions()
    .id(123)
    .one() %}
// Fetch a revision entry
$entries = \craft\elements\Entry::find()
    ->revisions()
    ->id(123)
    ->one();

:::

search

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

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

:::

section

Narrows the query results based on the sections the entries belong to.

Possible values include:

Value Fetches entries…
'foo' in a section with a handle of foo.
'not foo' not in a section with a handle of foo.
['foo', 'bar'] in a section with a handle of foo or bar.
['not', 'foo', 'bar'] not in a section with a handle of foo or bar.
a Section object in a section represented by the object.

::: code

{# Fetch entries in the Foo section #}
{% set entries = craft.entries()
    .section('foo')
    .all() %}
// Fetch entries in the Foo section
$entries = \craft\elements\Entry::find()
    ->section('foo')
    ->all();

:::

sectionId

Narrows the query results based on the sections the entries belong to, per the sections’ IDs.

Possible values include:

Value Fetches entries…
1 in a section with an ID of 1.
'not 1' not in a section with an ID of 1.
[1, 2] in a section with an ID of 1 or 2.
['not', 1, 2] not in a section with an ID of 1 or 2.

::: code

{# Fetch entries in the section with an ID of 1 #}
{% set entries = craft.entries()
    .sectionId(1)
    .all() %}
// Fetch entries in the section with an ID of 1
$entries = \craft\elements\Entry::find()
    ->sectionId(1)
    ->all();

:::

siblingOf

Narrows the query results to only entries that are siblings of another entry.

Possible values include:

Value Fetches entries…
1 beside the entry with an ID of 1.
a Entry object beside the entry represented by the object.

::: code

{# Fetch entries beside this one #}
{% set entries = craft.entries()
    .siblingOf(myEntry)
    .all() %}
// Fetch entries beside this one
$entries = \craft\elements\Entry::find()
    ->siblingOf($myEntry)
    ->all();

:::

site

Determines which site(s) the entries should be queried in.

The current site will be used by default.

Possible values include:

Value Fetches entries…
'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\elements\db\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. :::

::: code

{# Fetch entries from the Foo site #}
{% set entries = craft.entries()
    .site('foo')
    .all() %}
// Fetch entries from the Foo site
$entries = \craft\elements\Entry::find()
    ->site('foo')
    ->all();

:::

siteId

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

The current site will be used by default.

::: code

{# Fetch entries from the site with an ID of 1 #}
{% set entries = craft.entries()
    .siteId(1)
    .all() %}
// Fetch entries from the site with an ID of 1
$entries = \craft\elements\Entry::find()
    ->siteId(1)
    ->all();

:::

slug

Narrows the query results based on the entries’ slugs.

Possible values include:

Value Fetches entries…
'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.

::: code

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

{# Fetch the entry with that slug #}
{% set entry = craft.entries()
    .slug(requestedSlug|literal)
    .one() %}
// Get the requested entry slug from the URL
$requestedSlug = \Craft::$app->request->getSegment(3);

// Fetch the entry with that slug
$entry = \craft\elements\Entry::find()
    ->slug(\craft\helpers\Db::escapeParam($requestedSlug))
    ->one();

:::

status

Narrows the query results based on the entries’ statuses.

Possible values include:

Value Fetches entries…
'live' (default) that are live.
'pending' that are pending (enabled with a Post Date in the future).
'expired' that are expired (enabled with an Expiry Date in the past).
'disabled' that are disabled.
['live', 'pending'] that are live or pending.

::: code

{# Fetch disabled entries #}
{% set entries = craft.entries()
    .status('disabled')
    .all() %}
// Fetch disabled entries
$entries = \craft\elements\Entry::find()
    ->status('disabled')
    ->all();

:::

title

Narrows the query results based on the entries’ titles.

Possible values include:

Value Fetches entries…
'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.

::: code

{# Fetch entries with a title that contains "Foo" #}
{% set entries = craft.entries()
    .title('*Foo*')
    .all() %}
// Fetch entries with a title that contains "Foo"
$entries = \craft\elements\Entry::find()
    ->title('*Foo*')
    ->all();

:::

trashed

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

::: code

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

:::

type

Narrows the query results based on the entries’ entry types.

Possible values include:

Value Fetches entries…
'foo' of a type with a handle of foo.
'not foo' not of a type with a handle of foo.
['foo', 'bar'] of a type with a handle of foo or bar.
['not', 'foo', 'bar'] not of a type with a handle of foo or bar.
an EntryType object of a type represented by the object.

::: code

{# Fetch entries in the Foo section with a Bar entry type #}
{% set entries = craft.entries()
    .section('foo')
    .type('bar')
    .all() %}
// Fetch entries in the Foo section with a Bar entry type
$entries = \craft\elements\Entry::find()
    ->section('foo')
    ->type('bar')
    ->all();

:::

typeId

Narrows the query results based on the entries’ entry types, per the types’ IDs.

Possible values include:

Value Fetches entries…
1 of a type with an ID of 1.
'not 1' not of a type with an ID of 1.
[1, 2] of a type with an ID of 1 or 2.
['not', 1, 2] not of a type with an ID of 1 or 2.

::: code

{# Fetch entries of the entry type with an ID of 1 #}
{% set entries = craft.entries()
    .typeId(1)
    .all() %}
// Fetch entries of the entry type with an ID of 1
$entries = \craft\elements\Entry::find()
    ->typeId(1)
    ->all();

:::

uid

Narrows the query results based on the entries’ UIDs.

::: code

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

:::

unique

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.

::: code

{# Fetch unique entries across all sites #}
{% set entries = craft.entries()
    .site('*')
    .unique()
    .all() %}
// Fetch unique entries across all sites
$entries = \craft\elements\Entry::find()
    ->site('*')
    ->unique()
    ->all();

:::

uri

Narrows the query results based on the entries’ URIs.

Possible values include:

Value Fetches entries…
'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.

::: code

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

{# Fetch the entry with that URI #}
{% set entry = craft.entries()
    .uri(requestedUri|literal)
    .one() %}
// Get the requested URI
$requestedUri = \Craft::$app->request->getPathInfo();

// Fetch the entry with that URI
$entry = \craft\elements\Entry::find()
    ->uri(\craft\helpers\Db::escapeParam($requestedUri))
    ->one();

:::

with

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

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

::: code

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

:::