Element Queries
You can fetch elements (entries, categories, assets, etc.) in your templates or PHP code using element queries.
Working with element queries consists of three steps:
- Create the element query. You do this by calling a “factory function” that is named after the element type you are going to fetch. For example, if you want to fetch entries, you’d call
craft.entries()
, which returns a new entry query. - Set some parameters. By default, element queries will be configured to return all elements of the specified type. You can narrow that down to just the elements you care about by setting parameters on the query.
- Execute the query. Once you’ve specified the query parameters, you’re ready for Craft to fetch the elements and give you the results. You do that by calling
.all()
or.one()
, depending on whether you need multiple elements, or just one.
Here’s what a typical element query might look like:
{# Create an entry query and set some parameters on it #}
{% set entryQuery = craft.entries()
.section('news')
.orderBy('postDate DESC')
.limit(10) %}
{# Execute the query and get the results #}
{% set entries = entryQuery.all() %}
Each type of element has its own function for creating element queries, and they each have their own parameters you can set. See the individual element query pages for more details on working with them:
- Asset Queries
- Category Queries
- Entry Queries
- Global Set Queries
- Matrix Block Queries
- Tag Queries
- User Queries
Most custom fields support element query parameters as well, named after the field handles. See each field type’s documentation for examples.
# Executing Element Queries
Once you’ve defined your parameters on the query, there are multiple functions available to execute it, depending on what you need back.
# all()
Most of the time, you just want to get the elements that you’re querying for. You do that with the all()
function.
# one()
If you only need a single element, call one()
instead of all()
. It will either return the element or null
if no matching element exists.
# exists()
If you just need to check if any elements exist that match the element query, you can call exists()
, which will return either true
or false
.
# count()
If you want to know how many elements match your element query, you can call count()
.
The limit
and offset
parameters will be ignored when you call count()
.
# ids()
If you just want a list of matching element IDs, you can call ids()
.
# Caching Element Queries
# cache()
Craft’s element query results can be cached with the cache()
function:
This is a layer on top of data caching that’s different from the template {% cache %} tag.
Craft registers an ElementQueryTagDependency (opens new window) for you by default, so cache dependencies and invalidation are handled automatically.
# Advanced Element Queries
Element queries are specialized query builders (opens new window) under the hood, so they support most of the same methods provided by craft\db\Query (opens new window).
# Selections
- select() (opens new window)
- addSelect() (opens new window)
- distinct() (opens new window)
- groupBy() (opens new window)
# Joins
# Conditions
- where() (opens new window)
- andWhere() (opens new window)
- orWhere() (opens new window)
- filterWhere() (opens new window)
- andFilterWhere() (opens new window)
- orFilterWhere() (opens new window)
# Query Execution
- all() (opens new window)
- one() (opens new window)
- nth() (opens new window)
- exists() (opens new window)
- count() (opens new window)
- column() (opens new window)
- scalar() (opens new window)
- sum() (opens new window)
- average() (opens new window)
- min() (opens new window)
- max() (opens new window)
If you need to reference a custom field column in any of the above methods, you will need to use its complete column name (e.g. field_altText_xssyxqvs
).
When customizing an element query, you can call getRawSql() (opens new window) to get the full SQL that is going to be executed by the query, so you have a better idea of what to modify.
{{ dump(query.getRawSql()) }}