Entry Queries

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

{# Create a new entry query #}
{% set myEntryQuery = craft.entries() %}

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:

Param Description
after Narrows the query results to only entries that were posted on or after a certain date.
ancestorDist Narrows the query results to only entries that are up to a certain distance away from the entry specified by ancestorOf.
ancestorOf Narrows the query results to only entries that are ancestors of another entry.
anyStatus Clears out the status and enabledForSite parameters.
asArray Causes the query to return matching entries as arrays of data, rather than Entry objects.
authorGroup Narrows the query results based on the user group the entries’ authors belong to.
authorGroupId Narrows the query results based on the user group the entries’ authors belong to, per the groups’ IDs.
authorId Narrows the query results based on the entries’ authors.
before Narrows the query results to only entries that were posted before a certain date.
clearCachedResult Clears the cached result.
dateCreated Narrows the query results based on the entries’ creation dates.
dateUpdated Narrows the query results based on the entries’ last-updated dates.
descendantDist Narrows the query results to only entries that are up to a certain distance away from the entry specified by descendantOf.
descendantOf Narrows the query results to only entries that are descendants of another entry.
draftCreator Narrows the query results to only drafts created by a given user.
draftId Narrows the query results based on the entries’ draft’s ID (from the drafts table).
draftOf Narrows the query results to only drafts of a given entry.
drafts Narrows the query results to only drafts entries.
enabledForSite Narrows the query results based on whether the entries are enabled in the site they’re being queried in, per the site parameter.
expiryDate Narrows the query results based on the entries’ expiry dates.
fixedOrder Causes the query results to be returned in the order specified by id.
hasDescendants Narrows the query results based on whether the entries have any descendants.
id Narrows the query results based on the entries’ IDs.
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.
leaves Narrows the query results based on whether the entries are “leaves” (entries with no descendants).
level Narrows the query results based on the entries’ level within the structure.
limit Determines the number of entries that should be returned.
nextSiblingOf Narrows the query results to only the entry that comes immediately after another entry.
offset Determines how many entries should be skipped in the results.
orderBy Determines the order that the entries should be returned in. (If empty, defaults to postDate DESC.)
positionedAfter Narrows the query results to only entries that are positioned after another entry.
positionedBefore Narrows the query results to only entries that are positioned before another entry.
postDate Narrows the query results based on the entries’ post dates.
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 entry that comes immediately before another entry.
relatedTo Narrows the query results to only entries 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 entries’ revision’s ID (from the revisions table).
revisionOf Narrows the query results to only revisions of a given entry.
revisions Narrows the query results to only revision entries.
search Narrows the query results to only entries that match a search query.
section Narrows the query results based on the sections the entries belong to.
sectionId Narrows the query results based on the sections the entries belong to, per the sections’ IDs.
siblingOf Narrows the query results to only entries that are siblings of another entry.
site Determines which site(s) the entries should be queried in.
siteId Determines which site(s) the entries should be queried in, per the site’s ID.
slug Narrows the query results based on the entries’ slugs.
status Narrows the query results based on the entries’ statuses.
title Narrows the query results based on the entries’ titles.
trashed Narrows the query results to only entries that have been soft-deleted.
type Narrows the query results based on the entries’ entry types.
typeId Narrows the query results based on the entries’ entry types, per the types’ IDs.
uid Narrows the query results based on the entries’ UIDs.
unique Determines whether only elements with unique IDs should be returned by the query.
uri Narrows the query results based on the entries’ URIs.
with Causes the query to return matching entries eager-loaded with related elements.

# 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.
{# Fetch entries posted this month #}
{% set firstDayOfMonth = date('first day of this month') %}

{% set entries = craft.entries()
    .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.

{# Fetch entries above this one #}
{% set entries = craft.entries()
    .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.
{# Fetch entries above this one #}
{% set entries = craft.entries()
    .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.

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

# asArray

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

{# Fetch entries as arrays #}
{% set entries = craft.entries()
    .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.
{# Fetch entries with an author in the Foo user group #}
{% set entries = craft.entries()
    .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.
{# Fetch entries with an author in a group with an ID of 1 #}
{% set entries = craft.entries()
    .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.
{# Fetch entries with an author with an ID of 1 #}
{% set entries = craft.entries()
    .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.
{# Fetch entries posted before this month #}
{% set firstDayOfMonth = date('first day of this month') %}

{% set entries = craft.entries()
    .before(firstDayOfMonth)
    .all() %}

# clearCachedResult

Clears the cached result.

# 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.
{# 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() %}

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

{% set entries = craft.entries()
    .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.

{# Fetch entries below this one #}
{% set entries = craft.entries()
    .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.
{# Fetch entries below this one #}
{% set entries = craft.entries()
    .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\User object created by the user represented by the object.
{# Fetch drafts by the current user #}
{% set entries = craft.entries()
    .draftCreator(currentUser)
    .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.
{# Fetch a draft #}
{% set entries = craft.entries()
    .draftId(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.
{# Fetch drafts of the entry #}
{% set entries = craft.entries()
    .draftOf(myEntry)
    .all() %}

# drafts

Narrows the query results to only drafts entries.

{# Fetch a draft entry #}
{% set entries = {twig-function}
    .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.
{# Fetch all entries, including ones disabled for this site #}
{% set entries = craft.entries()
    .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.
{# Fetch entries expiring this month #}
{% set nextMonth = date('first day of next month')|atom %}

{% set entries = craft.entries()
    .expiryDate("< #{nextMonth}")
    .all() %}

# fixedOrder

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

{# Fetch entries in a specific order #}
{% set entries = craft.entries()
    .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.)

{# Fetch entries that have descendants #}
{% set entries = craft.entries()
    .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.
{# Fetch the entry by its ID #}
{% set entry = craft.entries()
    .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.

{# Fetch entries in reverse #}
{% set entries = craft.entries()
    .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.)

{# Fetch entries that have no descendants #}
{% set entries = craft.entries()
    .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.
{# Fetch entries positioned at level 3 or above #}
{% set entries = craft.entries()
    .level('>= 3')
    .all() %}

# limit

Determines the number of entries that should be returned.

{# Fetch up to 10 entries  #}
{% set entries = craft.entries()
    .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.
{# Fetch the next entry #}
{% set entry = craft.entries()
    .nextSiblingOf(myEntry)
    .one() %}

# offset

Determines how many entries should be skipped in the results.

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

# orderBy

Determines the order that the entries should be returned in. (If empty, defaults to postDate DESC.)

{# Fetch all entries in order of date created #}
{% set entries = craft.entries()
    .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.
{# Fetch entries after this one #}
{% set entries = craft.entries()
    .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.
{# Fetch entries before this one #}
{% set entries = craft.entries()
    .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.
{# 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() %}

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

{# 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() %}

# 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.
{# Fetch the previous entry #}
{% set entry = craft.entries()
    .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.

{# Fetch all entries that are related to myCategory #}
{% set entries = craft.entries()
    .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\User object created by the user represented by the object.
{# Fetch revisions by the current user #}
{% set entries = craft.entries()
    .revisionCreator(currentUser)
    .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.
{# Fetch a revision #}
{% set entries = craft.entries()
    .revisionId(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.
{# Fetch revisions of the entry #}
{% set entries = craft.entries()
    .revisionOf(myEntry)
    .all() %}

# revisions

Narrows the query results to only revision entries.

{# Fetch a revision entry #}
{% set entries = {twig-function}
    .revisions()
    .id(123)
    .one() %}

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.

{# 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() %}

# 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.
{# Fetch entries in the Foo section #}
{% set entries = craft.entries()
    .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.
{# Fetch entries in the section with an ID of 1 #}
{% set entries = craft.entries()
    .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.
{# Fetch entries beside this one #}
{% set entries = craft.entries()
    .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\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.

{# Fetch entries from the Foo site #}
{% set entries = craft.entries()
    .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.

{# Fetch entries from the site with an ID of 1 #}
{% set entries = craft.entries()
    .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.
{# 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() %}

# 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.
{# Fetch disabled entries #}
{% set entries = craft.entries()
    .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.
{# Fetch entries with a title that contains "Foo" #}
{% set entries = craft.entries()
    .title('*Foo*')
    .all() %}

# trashed

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

{# Fetch trashed entries #}
{% set entries = craft.entries()
    .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.
{# Fetch entries in the Foo section with a Bar entry type #}
{% set entries = craft.entries()
    .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.
{# Fetch entries of the entry type with an ID of 1 #}
{% set entries = craft.entries()
    .typeId(1)
    .all() %}

# uid

Narrows the query results based on the entries’ UIDs.

{# Fetch the entry by its UID #}
{% set entry = craft.entries()
    .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.

{# Fetch unique entries across all sites #}
{% set entries = craft.entries()
    .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.
{# Get the requested URI #}
{% set requestedUri = craft.app.request.getPathInfo() %}

{# Fetch the entry with that URI #}
{% set entry = craft.entries()
    .uri(requestedUri|literal)
    .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.

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