Skip to content

Instantly share code, notes, and snippets.

@troutacular
Last active April 6, 2017 23:58
Show Gist options
  • Select an option

  • Save troutacular/703ffcf99b9810812aef17be7e2beac4 to your computer and use it in GitHub Desktop.

Select an option

Save troutacular/703ffcf99b9810812aef17be7e2beac4 to your computer and use it in GitHub Desktop.
Add ACF to WP REST API
<?php
/**
* The functions to add all ACF types to the REST API responses.
*
* @link https://developer.wordpress.org/reference/hooks/rest_prepare_post_type/
* @package theme-name
*/
/**
* Add Options for Posts.
*/
add_filter( 'rest_prepare_post', function( $response ) {
$response->data['acf'] = get_fields( $response->data['id'] );
return $response;
} );
/**
* Add Options for Pages.
*/
add_filter( 'rest_prepare_page', function( $response ) {
$response->data['acf'] = get_fields( $response->data['id'] );
return $response;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment