Skip to content

Instantly share code, notes, and snippets.

@maximilliangeorge
Created October 31, 2018 14:14
Show Gist options
  • Save maximilliangeorge/305894858c6eb53cccac8245523edc44 to your computer and use it in GitHub Desktop.
Save maximilliangeorge/305894858c6eb53cccac8245523edc44 to your computer and use it in GitHub Desktop.
Add ACF custom fields to the Wordpress JSON API
<?php
// Add ACF data to the WP JSON REST API
add_action('rest_api_init', 'create_api_posts_meta_field');
function create_api_posts_meta_field() {
function get_acf($post) {
$acf = get_fields($post['ID']);
if (isset($post)) {
$post['acf'] = $acf;
}
return $post;
}
register_rest_field('post', 'acf', array(
'get_callback' => 'get_acf'
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment