How to expose all the ACF fields to Wordpress REST API in both pages and custom postypes
Asked Answered
P

4

21

I want to expose all the ACF fields that belong to a page or custom post type to the WordPress REST API in order to do some API calls through javascript.

The final expected result would be all the ACF fields inside an ACF object that you can easily access.

Phenocryst answered 6/6, 2019 at 8:44 Comment(2)
Let us know what you have tried so far.. show us some code.Orlina
@cbdev420 Hi, I created this question just to help other devs who stumble in the dark, answering it myself (see below).Phenocryst
P
19

Through the following code, you will be able to expose page and your custom postypes ACF fields in the wordpress REST API and access them inside the ACF object.

You can obviously customise the postypes to exclude or to include in the arrays: $postypes_to_exclude and $extra_postypes_to_include.

function create_ACF_meta_in_REST() {
    $postypes_to_exclude = ['acf-field-group','acf-field'];
    $extra_postypes_to_include = ["page"];
    $post_types = array_diff(get_post_types(["_builtin" => false], 'names'),$postypes_to_exclude);

    array_push($post_types, $extra_postypes_to_include);

    foreach ($post_types as $post_type) {
        register_rest_field( $post_type, 'ACF', [
            'get_callback'    => 'expose_ACF_fields',
            'schema'          => null,
       ]
     );
    }

}

function expose_ACF_fields( $object ) {
    $ID = $object['id'];
    return get_fields($ID);
}

add_action( 'rest_api_init', 'create_ACF_meta_in_REST' );

Here's the gist for reference: https://gist.github.com/MelMacaluso/6c4cb3db5ac87894f66a456ab8615f10

Phenocryst answered 6/6, 2019 at 8:47 Comment(0)
A
35

Another simple solution that is working perfect for me now. You can add the following function on functions.php or fields.php Using ACF getFields before sending a rest request. You can add this to any special page rest_prepare_page or rest_prepare_post.

The ACF data will be in the json response with key acf

// add this to functions.php
//register acf fields to Wordpress API
//https://support.advancedcustomfields.com/forums/topic/json-rest-api-and-acf/

function acf_to_rest_api($response, $post, $request) {
    if (function_exists('get_fields') && isset($post->id)) {
        $response->data['acf'] = get_fields($post->id);
    }
    return $response;
}
add_filter('rest_prepare_post', 'acf_to_rest_api', 10, 3);
Annotation answered 14/8, 2019 at 20:52 Comment(2)
This works great! Note that for custom posts type (cpt) the filter is rest_prepare_cptMoralez
And for anyone slow off the mark like me in PattyOK's comment cpt in rest_prepare_cpt means the slug of your cpt not literal string cptLunch
P
19

Through the following code, you will be able to expose page and your custom postypes ACF fields in the wordpress REST API and access them inside the ACF object.

You can obviously customise the postypes to exclude or to include in the arrays: $postypes_to_exclude and $extra_postypes_to_include.

function create_ACF_meta_in_REST() {
    $postypes_to_exclude = ['acf-field-group','acf-field'];
    $extra_postypes_to_include = ["page"];
    $post_types = array_diff(get_post_types(["_builtin" => false], 'names'),$postypes_to_exclude);

    array_push($post_types, $extra_postypes_to_include);

    foreach ($post_types as $post_type) {
        register_rest_field( $post_type, 'ACF', [
            'get_callback'    => 'expose_ACF_fields',
            'schema'          => null,
       ]
     );
    }

}

function expose_ACF_fields( $object ) {
    $ID = $object['id'];
    return get_fields($ID);
}

add_action( 'rest_api_init', 'create_ACF_meta_in_REST' );

Here's the gist for reference: https://gist.github.com/MelMacaluso/6c4cb3db5ac87894f66a456ab8615f10

Phenocryst answered 6/6, 2019 at 8:47 Comment(0)
D
5

ACF has the ability to have fields added to the REST API via each field as of version 5.11. You can review the update here: https://www.advancedcustomfields.com/resources/rest-api/

The gist is that each field has a setting for "Show in REST API". By default it is set to "No", but if you switch this to "Yes" it will be added to the REST data for each post/custom post type.

Downbeat answered 10/11, 2021 at 23:47 Comment(0)
R
1

You can use the following plugin to expose the ACF fields in REST.

https://wordpress.org/plugins/acf-to-rest-api/

If your ACF fields have a relationship and want to include those relationships in rest as well, you can use the following plugin.

https://github.com/airesvsg/acf-to-rest-api-recursive

Update: ACF has own settings to add ACF fields to REST Response. So, you don't need to use it.

Rafaelof answered 6/6, 2019 at 9:54 Comment(2)
Thanks, I was aware of those plugins and used them in the past. Unfortunately they are prone to break with major (but also minor) WordPress updates. The snippet of code too but slightly less as is using core vanilla WordPress functions.Phenocryst
This shouldn't be recommended anylonger as the ACF plugin now has its own setting to add ACF fields on page/post payloads. See answer by Ron: https://mcmap.net/q/594637/-how-to-expose-all-the-acf-fields-to-wordpress-rest-api-in-both-pages-and-custom-postypesPingpingpong

© 2022 - 2024 — McMap. All rights reserved.