Skip to content

Latest commit

 

History

History
72 lines (45 loc) · 2.09 KB

01-basic-usage.md

File metadata and controls

72 lines (45 loc) · 2.09 KB

Basic Usage

Installation

Install via Composer:

composer require samrap/acf-fluent

Core Concepts

All calls to ACF Fluent are made through the Samrap\Acf\Acf class. This class contains static factory methods that each return a Samrap\Acf\Fluent\Builder instance. The builder provides the fluent interface for getting and updating fields, sub fields and options, setting default values, adding constraints, and much more.

We can retrieve a builder instance for an ACF field by calling the Samrap\Acf\Acf::field method, passing in the name of the field as the argument. Let's take a look:

use Samrap\Acf\Acf;

$field = Acf::field('heading');

In the above example, we get a new Samrap\Acf\Fluent\Builder instance for an ACF field names heading. We can call the get method on the builder to retrieve the field:

use Samrap\Acf\Acf;

$heading = Acf::field('heading')->get();

The same can be done for sub fields just as easily via the subField method:

use Samrap\Acf\Acf;

$content = Acf::subField('content')->get();

And even global option fields:

use Samrap\Acf\Acf;

$toggle = Acf::option('toggle')->get();

Alternatively, you can use the fluent_* helper functions to return a builder instance. The following functions will return a new builder for fields, sub fields, and options respectively:

fluent_field('name');
fluent_sub_field('name');
fluent_option('name');

The field can then be retrieved by calling the get method as usual:

$heading = fluent_field('heading')->get();

Note: The fluent helper functions are not loaded by default. In order to use them, the vendor/samrap/acf-fluent/src/helpers.php file will need to be included in your theme.


The real power of ACF Fluent comes when we make full use of the builder. It provides useful methods that can be chained together to build powerful ACF "queries". Next, we will cover what each of these methods do and how to make use of them.

Builder Methods