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:

after

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

Possible values include:

ValueFetches entries…
'2018-04-01'that were posted after 2018-04-01.
a DateTime objectthat 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:

ValueFetches entries…
1above the entry with an ID of 1.
a Entry objectabove 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:

ValueFetches 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 objectwith 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:

ValueFetches entries…
1with 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:

ValueFetches entries…
1with 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:

ValueFetches entries…
'2018-04-01'that were posted before 2018-04-01.
a DateTime objectthat 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() %}

dateCreated

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

Possible values include:

ValueFetches 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:

ValueFetches 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:

ValueFetches entries…
1below the entry with an ID of 1.
a Entry objectbelow 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.

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:

ValueFetches entries…
true(default)that are enabled in the site.
falsewhether 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:

ValueFetches entries…
'>= 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:

ValueFetches entries…
1with 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.

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:

ValueFetches entries…
1with 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:

ValueFetches the entry…
1after the entry with an ID of 1.
a Entry objectafter 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.

{# Fetch all entries in order of date created #}
{% set entries = craft.entries()
    .orderBy('elements.dateCreated asc')
    .all() %}

positionedAfter

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

Possible values include:

ValueFetches entries…
1after the entry with an ID of 1.
a Entry objectafter 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:

ValueFetches entries…
1before the entry with an ID of 1.
a Entry objectbefore 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:

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

prevSiblingOf

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

Possible values include:

ValueFetches the entry…
1before the entry with an ID of 1.
a Entry objectbefore 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() %}

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

ValueFetches 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 objectin 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:

ValueFetches entries…
1in 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:

ValueFetches entries…
1beside the entry with an ID of 1.
a Entry objectbeside the entry represented by the object.
{# Fetch entries beside this one #}
{% set entries = craft.entries()
    .siblingOf(myEntry)
    .all() %}

site

Determines which site the entries should be queried in.

The current site will be used by default.

Possible values include:

ValueFetches entries…
'foo'from the site with a handle of foo.
a \craft\elements\db\Site objectfrom the site represented by the object.
{# Fetch entries from the Foo site #}
{% set entries = craft.entries()
    .site('foo')
    .all() %}

siteId

Determines which site 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:

ValueFetches 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:

ValueFetches 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 = {twig-function}
    .status('disabled')
    .all() %}

title

Narrows the query results based on the entries’ titles.

Possible values include:

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

type

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

Possible values include:

ValueFetches 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 objectof 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:

ValueFetches entries…
1of 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() %}

uri

Narrows the query results based on the entries’ URIs.

Possible values include:

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