ACF_Rest_Api::load_fields
Load field values into the requested object. This method is not a part of any public API and is only public as it is required by WordPress.
Method of the class: ACF_Rest_Api{}
No Hooks.
Returns
Array.
Usage
$ACF_Rest_Api = new ACF_Rest_Api(); $ACF_Rest_Api->load_fields( $object, $field_name, $request, $object_sub_type );
- $object(array) (required)
- An array representation of the post, term, or user object.
- $field_name(string) (required)
- .
- $request(WP_REST_Request) (required)
- .
- $object_sub_type(string) (required)
- Note that this isn't the same as
$this->object_type. This variable is more specific and can be a post type or taxonomy.
ACF_Rest_Api::load_fields() ACF Rest Api::load fields code ACF 6.0.4
public function load_fields( $object, $field_name, $request, $object_sub_type ) {
// The fields loaded for display on the REST API in the form of {$field_name}=>{$field_value} pairs.
$fields = array();
// Determine the object ID from the given object.
$object_id = acf_get_object_id( $object );
// Use this object type parsed from the request.
$object_type = $this->request->object_type;
// Object ID and type are essential to determining which fields to load. Return if we don't have both.
if ( ! $object_id or ! $object_type ) {
return $fields;
}
$object_sub_type = str_replace( '-revision', '', $object_sub_type );
// Get all field groups for the current object.
$field_groups = $this->get_field_groups_by_id( $object_id, $object_type, $object_sub_type );
if ( empty( $field_groups ) ) {
return $fields;
}
// Determine the ACF ID string for the current object.
$post_id = $this->make_identifier( $object_id, $object_type );
// Loop through the fields within all applicable field groups and add the fields to the response.
foreach ( $field_groups as $field_group ) {
foreach ( $this->get_fields( $field_group, $object_id ) as $field ) {
$value = acf_get_value( $post_id, $field );
if ( $this->embed_links ) {
$this->embed_links->prepare_links( $post_id, $field );
}
// Format the field value according to the request params.
$format = $request->get_param( 'acf_format' ) ?: acf_get_setting( 'rest_api_format' );
$value = acf_format_value_for_rest( $value, $post_id, $field, $format );
$fields[ $field['name'] ] = $value;
}
}
/**
* Reset the store so that REST API values (which may be preloaded
* by WP core and have different values than standard values) aren't
* saved to the store.
*/
acf_get_store( 'values' )->reset();
return $fields;
}