References to Global fields
Last updated
Last updated
Global fields references in ACF are meant as puzzle pieces for building your field definitions for a specific scope, making it easy to reference content that would otherwise be replicated across many products or collections, etc. For example. you have a set of features with an icon and a description common to your products defined as Global fields:
You can then use a global reference field to point to the section or any of the fields (and even specific occurrences within both), allowing for a single point of maintenance of the underlying data. So if a feature icon or description changes, you just update the global field and the change will be reflected on all your products instantly.
Tip: If a repeatable section contains a field of type Text, the content of the first in the sequence will be included after each occurrence's index number ("Organic" and "Vegan" in the example) for easier selection of the correct occurrence
The underlying Metafield value when selecting global fields will always be a JSON object with a single .references property containing an iterable array of JSON objects with either a .field or a .section property (depending on what is being referenced) and optionally either an .index or .key property. The .key property is only used for repeatable section references and only when the global section being referenced has its "Use sticky references" setting enabled (see below). If just a single field or section is selected, the array will have just one entry. This allows for consistent use of Liquid's array filters such as first, last, where, etc. Example structure:
You can query the .references array for either the .field or .section property being present to detect what is being referenced as well as the .index property to detect if a specific occurrence is being referenced.
When one or more fields are being referenced, each field reference (an element in the .references array) will contain the field's name in the .field property and you can resolve the reference in Liquid like this:
Or, if you have selected multiple fields references:
If you are referencing repeatable fields, please see the related article "Repeatable fields" for instructions about how to loop over these
A global reference may point to specific occurrences of repeatable fields. When this is the case, the referenced index is stored in the .index property. This example covers a single field reference to a specific occurrence:
If you need to check which type of field is being referenced, you can cross-reference the .field selection against the field definitions for the global scope, like this:
If the global reference field is referencing a .section, you can go via the field definitions to find the section's fields (see related article below), like this:
If you are referencing a repeatable section, please see the related article "Repeatable fields" for instructions about how to loop over these
Reference fields may point to specific occurrences of repeatable sections. When this is the case, the referenced index is stored in the .index property and is valid for accessing the relevant part of the section's fields. This example covers a single reference to a section's specific occurrence (like "Features #1" in the above selection) which then is resolved to the contained fields dynamically:
Usually, though, you'll be selecting specific sections and just want to use the index of the section being referenced (like "Features #1" and "Features #2" in the above selection), so you can loop the reference field selections and code the fields directly in Liquid using the index property:
Same as above, reference fields may point to specific occurrences of repeatable sections but can optionally store a "sticky" reference to the selected occurrences' index. This happens automatically when the global section being referenced has its "Use sticky references" setting enabled. When this is the case, the referenced "index" is stored indirectly in the .key property and the correct index can then be determined dynamically in Liquid.
This is the recommended way to use section references because it ensures that the references are always up to date even if the occurrences within the global section are rearranged after selections have been stored in the reference field.
This example covers a single reference to a section's specific occurrence (like "Features #1" in the above selection) via a key. The actual occurrence index is then resolved in Liquid via the 'assign index =' statement:
If you get lost in field references and resolving to global fields, try outputting the involved fields using Liquid's json filter. This will often reveal any issues