Dot All Lisbon – the official Craft CMS conference – is happening September 23 - 25.

Globals

Globals store content that is available globally throughout your templates but is not tied to any one URL.

Craft organizes globals into global sets. Each global set has its own field layout and can use any of the built-in or plugin-provided custom fields.

To create a global get, go to SettingsGlobals.

If you have at least one Global Set, Craft will add a new “Globals” item to the main control panel navigation. Clicking this will take you to a page that lists all your global sets in a sidebar, as well as all of the fields associated with the selected global set in the main content area.

Unlike entries, global sets don’t have the Live Preview feature, since they aren’t associated with any one particular URL.

With the release of Craft 4.4, we began consolidating features of other element types into entries.

As part of that process, we introduced a console command that can automate the conversion of global sets to entries:

php craft entrify/global-set myGlobalSetHandle

You will be given an opportunity to migrate the global set into a new or existing single. The Title field (and Status controls <Since ver="4.5.0" feature="Entry status control visibility setting" />) for the single’s new entry type will be disabled, to maintain parity with the legacy globals interface.

Read more about this transition on our blog.

Global Sets in Templates

You can access your global sets from any template via their handles.

If you have a global set with the handle companyInfo and it has a field with the handle yearEstablished, you can access that field anywhere using this code:

{{ companyInfo.yearEstablished }}

For additional global set properties you can use besides your custom fields see craft4:craft\elements\GlobalSet for a full reference.

Manually Loading Global Sets

In some special situations, like within email templates, global sets won’t be available by default. Any global set may still be loaded manually. The above example could be loaded with getSetByHandle():

{% set companyInfo = craft.app.globals().getSetByHandle('companyInfo') %}
$companyInfo = \Craft::$app->getGlobals()->getSetByHandle('companyInfo');

More details are available in the Globals service class documentation.

Global Sets with Multiple Sites

If you run multiple sites with Craft, global sets are available in all sites. However, you can set the values in those sets on a per site basis, even leaving some fields blank, if desired.

To do that, edit the global set’s fields, and make sure that their “Translation Method” settings are set to “Translate for each site”.

To toggle between sites while viewing global sets, use the dropdown menu at the top left of the global sets page in the control panel.

Toggling between sites in Globals

Querying Globals

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() %}
// Create a new global set query
$myGlobalSetQuery = \craft\elements\GlobalSet::find();

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.

See 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 #}

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:

| Param | Description | ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | andNotRelatedTo | Narrows the query results to only global sets that are not related to certain other elements. | andRelatedTo | Narrows the query results to only global sets that are related to certain other elements. | andWith | Causes the query to return matching global sets eager-loaded with related elements, in addition to the elements that were already specified by with. | asArray | Causes the query to return matching global sets as arrays of data, rather than GlobalSet objects. | cache | Enables query cache for this Query. | canonicalsOnly | Narrows the query results to only canonical elements, including elements that reference another canonical element via canonicalId so long as they aren’t a draft. | clearCachedResult | Clears the cached result. | dateCreated | Narrows the query results based on the global sets’ creation dates. | dateUpdated | Narrows the query results based on the global sets’ last-updated dates. | eagerly | Causes the query to be used to eager-load results for the query’s source element and any other elements in its collection. | fixedOrder | Causes the query results to be returned in the order specified by id. | getFieldLayouts | Returns the field layouts that could be associated with the resulting elements. | handle | Narrows the query results based on the global sets’ handles. | id | Narrows the query results based on the global sets’ IDs. | 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(). | inBulkOp | Narrows the query results to only global sets that were involved in a bulk element operation. | inReverse | Causes the query results to be returned in reverse order. | language | Determines which site(s) the global sets should be queried in, based on their language. | limit | Determines the number of global sets that should be returned. | notRelatedTo | Narrows the query results to only global sets that are not related to certain other elements. | offset | Determines how many global sets should be skipped in the results. | orderBy | Determines the order that the global sets should be returned in. (If empty, defaults to sortOrder ASC.) | preferSites | If unique is set, this determines which site should be selected when querying multi-site elements. | prepForEagerLoading | Prepares the query for lazy eager loading. | prepareSubquery | Prepares the element query and returns its subquery (which determines what elements will be returned). | relatedTo | Narrows the query results to only global sets that are related to certain other elements. | render | Executes the query and renders the resulting elements using their partial templates. | search | Narrows the query results to only global sets that match a search query. | site | Determines which site(s) the global sets should be queried in. | siteId | Determines which site(s) the global sets should be queried in, per the site’s ID. | siteSettingsId | Narrows the query results based on the global sets’ IDs in the elements_sites table. | trashed | Narrows the query results to only global sets that have been soft-deleted. | uid | Narrows the query results based on the global sets’ UIDs. | unique | Determines whether only elements with unique IDs should be returned by the query. | wasCountEagerLoaded | Returns whether the query result count was already eager loaded by the query's source element. | wasEagerLoaded | Returns whether the query results were already eager loaded by the query's source element. | with | Causes the query to return matching global sets eager-loaded with related elements. | withCustomFields | Sets whether custom fields should be factored into the query. | withProvisionalDrafts | Causes the query to return provisional drafts for the matching elements, when they exist for the current user.

andNotRelatedTo

Defined by craft\elements\db\ElementQuery

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

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

::: code

{# Fetch all global sets that are related to myCategoryA and not myCategoryB #}
{% set globalSets = craft.globalSets()
  .relatedTo(myCategoryA)
  .andNotRelatedTo(myCategoryB)
  .all() %}
// Fetch all global sets that are related to $myCategoryA and not $myCategoryB
$globalSets = \craft\elements\GlobalSet::find()
    ->relatedTo($myCategoryA)
    ->andNotRelatedTo($myCategoryB)
    ->all();

:::

andRelatedTo

Defined by craft\elements\db\ElementQuery

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.

::: code

{# Fetch all global sets that are related to myCategoryA and myCategoryB #}
{% set globalSets = craft.globalSets()
  .relatedTo(myCategoryA)
  .andRelatedTo(myCategoryB)
  .all() %}
// Fetch all global sets that are related to $myCategoryA and $myCategoryB
$globalSets = \craft\elements\GlobalSet::find()
    ->relatedTo($myCategoryA)
    ->andRelatedTo($myCategoryB)
    ->all();

:::

andWith

Defined by craft\elements\db\ElementQuery

Causes the query to return matching global sets eager-loaded with related elements, in addition to the elements that were already specified by with.

.

asArray

Defined by craft\elements\db\ElementQuery

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

::: code

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

:::

cache

Defined by craft\elements\db\ElementQuery

Enables query cache for this Query.

canonicalsOnly

Defined by craft\elements\db\ElementQuery

Narrows the query results to only canonical elements, including elements that reference another canonical element via canonicalId so long as they aren’t a draft.

Unpublished drafts can be included as well if drafts(null) and draftOf(false) are also passed.

clearCachedResult

Defined by craft\elements\db\ElementQuery

Clears the cached result.

dateCreated

Defined by craft\elements\db\ElementQuery

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

Possible values include:

| Value | Fetches 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. | now/today/tomorrow/yesterday | that were created at midnight of the specified relative date.

::: code

{# 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() %}
// Fetch global sets 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);

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

:::

dateUpdated

Defined by craft\elements\db\ElementQuery

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

Possible values include:

| Value | Fetches 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. | now/today/tomorrow/yesterday | that were updated at midnight of the specified relative date.

::: code

{# Fetch global sets updated in the last week #}
{% set lastWeek = date('1 week ago')|atom %}

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

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

:::

eagerly

Defined by craft\elements\db\ElementQuery

Causes the query to be used to eager-load results for the query’s source element and any other elements in its collection.

fixedOrder

Defined by craft\elements\db\ElementQuery

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

::: tip If no IDs were passed to id, setting this to true will result in an empty result set. :::

::: code

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

:::

getFieldLayouts

Defined by craft\elements\db\ElementQuery

Returns the field layouts that could be associated with the resulting elements.

handle

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

Possible values include:

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

::: code

{# Fetch the global set with a handle of 'foo' #}
{% set globalSet = craft.globalSets()
  .handle('foo')
  .one() %}
// Fetch the global set with a handle of 'foo'
$globalSet = \craft\elements\GlobalSet::find()
    ->handle('foo')
    ->one();

:::

id

Defined by craft\elements\db\ElementQuery

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

Possible values include:

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

:::

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

ignorePlaceholders

Defined by craft\elements\db\ElementQuery

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().

inBulkOp

Defined by craft\elements\db\ElementQuery

Narrows the query results to only global sets that were involved in a bulk element operation.

inReverse

Defined by craft\elements\db\ElementQuery

Causes the query results to be returned in reverse order.

::: code

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

:::

language

Defined by craft\elements\db\ElementQuery

Determines which site(s) the global sets should be queried in, based on their language.

Possible values include:

| Value | Fetches global sets… | - | - | 'en' | from sites with a language of en. | ['en-GB', 'en-US'] | from sites with a language of en-GB or en-US. | ['not', 'en-GB', 'en-US'] | not in sites with a language of en-GB or en-US.

::: tip Elements that belong to multiple sites will be returned multiple times by default. If you only want unique elements to be returned, use unique in conjunction with this. :::

::: code

{# Fetch global sets from English sites #}
{% set globalSets = craft.globalSets()
  .language('en')
  .all() %}
// Fetch global sets from English sites
$globalSets = \craft\elements\GlobalSet::find()
    ->language('en')
    ->all();

:::

limit

Defined by yii\db\QueryTrait

Determines the number of global sets that should be returned.

::: code

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

:::

notRelatedTo

Defined by craft\elements\db\ElementQuery

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

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

::: code

{# Fetch all global sets that are related to myEntry #}
{% set globalSets = craft.globalSets()
  .notRelatedTo(myEntry)
  .all() %}
// Fetch all global sets that are related to $myEntry
$globalSets = \craft\elements\GlobalSet::find()
    ->notRelatedTo($myEntry)
    ->all();

:::

offset

Defined by yii\db\QueryTrait

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

::: code

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

:::

orderBy

Defined by craft\elements\db\ElementQuery

Determines the order that the global sets should be returned in. (If empty, defaults to sortOrder ASC.)

::: code

{# Fetch all global sets in order of date created #}
{% set globalSets = craft.globalSets()
  .orderBy('dateCreated ASC')
  .all() %}
// Fetch all global sets in order of date created
$globalSets = \craft\elements\GlobalSet::find()
    ->orderBy('dateCreated ASC')
    ->all();

:::

preferSites

Defined by craft\elements\db\ElementQuery

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 B, and Bar will be returned for Site C.

If this isn’t set, then preference goes to the current site.

::: code

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

:::

prepForEagerLoading

Defined by craft\elements\db\ElementQuery

Prepares the query for lazy eager loading.

prepareSubquery

Defined by craft\elements\db\ElementQuery

Prepares the element query and returns its subquery (which determines what elements will be returned).

relatedTo

Defined by craft\elements\db\ElementQuery

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.

::: code

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

:::

render

Defined by craft\elements\db\ElementQuery

Executes the query and renders the resulting elements using their partial templates.

If no partial template exists for an element, its string representation will be output instead.

search

Defined by craft\elements\db\ElementQuery

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.

::: code

{# 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() %}
// Get the search query from the 'q' query string param
$searchQuery = \Craft::$app->request->getQueryParam('q');

// Fetch all global sets that match the search query
$globalSets = \craft\elements\GlobalSet::find()
    ->search($searchQuery)
    ->all();

:::

site

Defined by craft\elements\db\ElementQuery

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

The current site will be used by default.

Possible values include:

| Value | Fetches 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\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. :::

::: code

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

:::

siteId

Defined by craft\elements\db\ElementQuery

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

The current site will be used by default.

Possible values include:

| Value | Fetches global sets… | - | - | 1 | from the site with an ID of 1. | [1, 2] | from a site with an ID of 1 or 2. | ['not', 1, 2] | not in a site with an ID of 1 or 2. | '*' | from any site.

::: code

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

:::

siteSettingsId

Defined by craft\elements\db\ElementQuery

Narrows the query results based on the global sets’ IDs in the elements_sites table.

Possible values include:

| Value | Fetches global sets… | - | - | 1 | with an elements_sites ID of 1. | 'not 1' | not with an elements_sites ID of 1. | [1, 2] | with an elements_sites ID of 1 or 2. | ['not', 1, 2] | not with an elements_sites ID of 1 or 2.

::: code

{# Fetch the global set by its ID in the elements_sites table #}
{% set globalSet = craft.globalSets()
  .siteSettingsId(1)
  .one() %}
// Fetch the global set by its ID in the elements_sites table
$globalSet = \craft\elements\GlobalSet::find()
    ->siteSettingsId(1)
    ->one();

:::

trashed

Defined by craft\elements\db\ElementQuery

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

::: code

{# Fetch trashed global sets #}
{% set globalSets = craft.globalSets()
  .trashed()
  .all() %}
// Fetch trashed global sets
$globalSets = \craft\elements\GlobalSet::find()
    ->trashed()
    ->all();

:::

uid

Defined by craft\elements\db\ElementQuery

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

::: code

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

:::

unique

Defined by craft\elements\db\ElementQuery

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 global sets across all sites #}
{% set globalSets = craft.globalSets()
  .site('*')
  .unique()
  .all() %}
// Fetch unique global sets across all sites
$globalSets = \craft\elements\GlobalSet::find()
    ->site('*')
    ->unique()
    ->all();

:::

wasCountEagerLoaded

Defined by craft\elements\db\ElementQuery

Returns whether the query result count was already eager loaded by the query's source element.

wasEagerLoaded

Defined by craft\elements\db\ElementQuery

Returns whether the query results were already eager loaded by the query's source element.

with

Defined by craft\elements\db\ElementQuery

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.

::: code

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

:::

withCustomFields

Defined by craft\elements\db\ElementQuery

Sets whether custom fields should be factored into the query.

withProvisionalDrafts

Defined by craft\elements\db\ElementQuery

Causes the query to return provisional drafts for the matching elements, when they exist for the current user.