Skip to content

Instantly share code, notes, and snippets.

@troutacular
Created April 17, 2017 20:13
Show Gist options
  • Save troutacular/cf429aac17c147015bfe17944b0b7e0e to your computer and use it in GitHub Desktop.
Save troutacular/cf429aac17c147015bfe17944b0b7e0e to your computer and use it in GitHub Desktop.
ACF in WP API Response
<?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