Home › Forums › Feature Requests › REST API › Reply To: REST API
Just a small update on this.
I ended up not using a plugin at all, but instead just adding the ACF fields by batch like this:
add_action( 'rest_api_init', 'slug_register_acf' );
function slug_register_acf() {
$post_types = get_post_types(['public'=>true], 'names');
foreach ($post_types as $type) {
register_api_field( $type,
'acf',
array(
'get_callback' => 'slug_get_acf',
'update_callback' => null,
'schema' => null,
)
);
}
}
function slug_get_acf( $object, $field_name, $request ) {
return get_fields($object[ 'id' ]);
}
This appends a key called “acf” to the rest api output for all public post types.
Will be easier to customise too if you only want certain fields to be exposed.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.