php - How to expose all the ACF fields to Wordpress REST API in both pages and custom postypes - Stack Overflow

PHOTO EMBED

Wed Nov 30 2022 17:21:52 GMT+0000 (UTC)

Saved by @nataliaferraz #php #wp #acf #rest

// 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')) return $response;

    if (isset($post)) {
        $acf = get_fields($post->id);
        $response->data['acf'] = $acf;
    }
    return $response;
}
add_filter('rest_prepare_post', 'acf_to_rest_api', 10, 3);
content_copyCOPY

add this to any special page rest_prepare_page or rest_prepare_post.

https://stackoverflow.com/questions/56473929/how-to-expose-all-the-acf-fields-to-wordpress-rest-api-in-both-pages-and-custom