Global Set Queries

You can fetch global sets in your templates or PHP code using global set queries.

{# Create a new global set query #}
{% set myGlobalSetQuery = craft.globalSets() %}

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

TIP

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

Example

We can load a global set from the primary site and display its content by doing the following:

  1. Create a global set query with craft.globalSets().
  2. Set the handle and siteId parameters on it.
  3. Fetch the global set with .one().
  4. Output its content.
{# Create a global set query with the 'handle' and 'siteId' parameters #}
{% set myGlobalSetQuery = craft.globalSets()
    .handle('footerCopy')
    .siteId(1) %}

{# Fetch the global set #}
{% set globalSet = myGlobalSetQuery.one() %}

{# Display the content #}
<p>{{ globalSet.copyrightInfo }}</p>

TIP

All global sets are already available as global variables to Twig templates. So you only need to fetch them through craft.globalSets() if you need to access their content for a different site than the current site.

Parameters

Global set queries support the following parameters:

anyStatus

Clears out the status() and enabledForSite() parameters.

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

asArray

Causes the query to return matching global sets as arrays of data, rather than GlobalSet objects.

{# Fetch global sets as arrays #}
{% set globalSets = craft.globalSets()
    .asArray()
    .all() %}

dateCreated

Narrows the query results based on the global sets’ creation dates.

Possible values include:

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

{% set globalSets = craft.globalSets()
    .dateCreated(['and', ">= #{start}", "< #{end}"])
    .all() %}

dateUpdated

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

Possible values include:

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

{% set globalSets = craft.globalSets()
    .dateUpdated(">= #{lastWeek}")
    .all() %}

draftCreator

Narrows the query results to only drafts created by a given user.

Possible values include:

ValueFetches drafts…
1created by the user with an ID of 1.
a \craft\elements\db\User objectby the user represented by the object.
{# Fetch drafts by the current user #}
{% set globalSets = craft.globalSets()
    .draftCreator(currentUser)
    .all() %}

draftId

Narrows the query results based on the global sets’ draft’s ID (from the drafts table).

Possible values include:

ValueFetches drafts…
1for the draft with an ID of 1.
{# Fetch a draft #}
{% set globalSets = craft.globalSets()
    .draftId(10)
    .all() %}

draftOf

Narrows the query results to only drafts of a given global set.

Possible values include:

ValueFetches drafts…
1for the global set with an ID of 1.
a GlobalSet objectfor the global set represented by the object.
{# Fetch drafts of the global set #}
{% set globalSets = craft.globalSets()
    .draftOf(myGlobalSet)
    .all() %}

drafts

Narrows the query results to only drafts global sets.

{# Fetch a draft global set #}
{% set globalSets = {twig-function}
    .drafts()
    .id(123)
    .one() %}

fixedOrder

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

{# Fetch global sets in a specific order #}
{% set globalSets = craft.globalSets()
    .id([1, 2, 3, 4, 5])
    .fixedOrder()
    .all() %}

handle

Narrows the query results based on the global sets’ handles.

Possible values include:

ValueFetches global sets…
'foo'with a handle of foo.
'not foo'not with a handle of foo.
['foo', 'bar']with a handle of foo or bar.
['not', 'foo', 'bar']not with a handle of foo or bar.
{# Fetch the global set with a handle of 'foo' #}
{% set globalSet = craft.globalSets()
    .handle('foo')
    .one() %}

id

Narrows the query results based on the global sets’ IDs.

Possible values include:

ValueFetches global sets…
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 global set by its ID #}
{% set globalSet = craft.globalSets()
    .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 global sets 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 global sets in reverse #}
{% set globalSets = craft.globalSets()
    .inReverse()
    .all() %}

limit

Determines the number of global sets that should be returned.

{# Fetch up to 10 global sets  #}
{% set globalSets = craft.globalSets()
    .limit(10)
    .all() %}

offset

Determines how many global sets should be skipped in the results.

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

orderBy

Determines the order that the global sets should be returned in.

{# Fetch all global sets in order of date created #}
{% set globalSets = craft.globalSets()
    .orderBy('dateCreated asc')
    .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 global sets from Site A, or Site B if they don’t exist in Site A #}
{% set globalSets = craft.globalSets()
    .site('*')
    .unique()
    .preferSites(['a', 'b'])
    .all() %}

relatedTo

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

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

{# Fetch all global sets that are related to myCategory #}
{% set globalSets = craft.globalSets()
    .relatedTo(myCategory)
    .all() %}

revisionCreator

Narrows the query results to only revisions created by a given user.

Possible values include:

ValueFetches revisions…
1created by the user with an ID of 1.
a \craft\elements\db\User objectby the user represented by the object.
{# Fetch revisions by the current user #}
{% set globalSets = craft.globalSets()
    .revisionCreator(currentUser)
    .all() %}

revisionId

Narrows the query results based on the global sets’ revision’s ID (from the revisions table).

Possible values include:

ValueFetches revisions…
1for the revision with an ID of 1.
{# Fetch a revision #}
{% set globalSets = craft.globalSets()
    .revisionId(10)
    .all() %}

revisionOf

Narrows the query results to only revisions of a given global set.

Possible values include:

ValueFetches revisions…
1for the global set with an ID of 1.
a GlobalSet objectfor the global set represented by the object.
{# Fetch revisions of the global set #}
{% set globalSets = craft.globalSets()
    .revisionOf(myGlobalSet)
    .all() %}

revisions

Narrows the query results to only revision global sets.

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

Narrows the query results to only global sets 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 global sets that match the search query #}
{% set globalSets = craft.globalSets()
    .search(searchQuery)
    .all() %}

site

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

The current site will be used by default.

Possible values include:

ValueFetches global sets…
'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 objectfrom 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 global sets from the Foo site #}
{% set globalSets = craft.globalSets()
    .site('foo')
    .all() %}

siteId

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

The current site will be used by default.

{# Fetch global sets from the site with an ID of 1 #}
{% set globalSets = craft.globalSets()
    .siteId(1)
    .all() %}

trashed

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

{# Fetch trashed global sets #}
{% set globalSets = craft.globalSets()
    .trashed()
    .all() %}

uid

Narrows the query results based on the global sets’ UIDs.

{# Fetch the global set by its UID #}
{% set globalSet = craft.globalSets()
    .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 global sets across all sites #}
{% set globalSets = craft.globalSets()
    .site('*')
    .unique()
    .all() %}

with

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

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

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