Skip to content

Instantly share code, notes, and snippets.

@ChrisLTD
Last active May 23, 2017 15:16
Show Gist options
  • Save ChrisLTD/b5cd37653010c2c91b7b28488412bafd to your computer and use it in GitHub Desktop.
Save ChrisLTD/b5cd37653010c2c91b7b28488412bafd to your computer and use it in GitHub Desktop.
Add ACF Fields to WP API responses
<?php
/*
* Add ACF Fields to REST responses
* Modified from: https://support.advancedcustomfields.com/forums/topic/rest-api/#post-32890
*/
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_rest_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' ]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment