Solvedacf to rest api How to get ACF post object field with ACF fields
βοΈAccepted Answer
Hi @travis-zookacreative,
Thanks for using my plugin, bellow I wrote an example most generic.
V3
add_filter( 'acf/rest_api/{type}/get_fields', function( $data ) {
if ( ! empty( $data ) ) {
array_walk_recursive( $data, 'get_fields_recursive' );
}
return $data;
} );
V2
add_filter( 'acf/rest_api/{type}/get_fields', function( $data, $request, $response ) {
if ( $response instanceof WP_REST_Response ) {
$data = $response->get_data();
}
if ( ! empty( $data ) ) {
array_walk_recursive( $data, 'get_fields_recursive' );
}
return $data;
}, 10, 3 );
Function to use for both versions:
function get_fields_recursive( $item ) {
if ( is_object( $item ) ) {
$item->acf = array();
if ( $fields = get_fields( $item ) ) {
$item->acf = $fields;
array_walk_recursive( $item->acf, 'get_fields_recursive' );
}
}
}
Other Answers:
Instead of defining it for every possible post type. I added a $types
array so it loops through the types and also uses it for the array_walk_recursive()
function.
/**
* ACF Data in Post Object Response
*/
$types = ['post', 'page', 'product']
foreach ($types as $type) {
add_filter( 'acf/rest_api/'.$type.'/get_fields', function( $data, $response ) use ($types) {
if ( $response instanceof WP_REST_Response ) {
$data = $response->get_data();
}
array_walk_recursive( $data, 'deepIncludeACFFields', $types );
return $data;
}, 10, 3 );
}
function deepIncludeACFFields( &$item, $key, $postTypes ) {
if ( isset( $item->post_type ) && in_array( $item->post_type, $postTypes ) ) {
$item->acf = get_fields( $item->ID );
}
}
I like @StevenDaoud's solution for adding the fields to the top level object. I opted to follow the plugin's format of placing all the fields in the acf
key. Both adding to the response as opposed to replacing it.
Also, I was getting an error with function( $data, $request, $response )
so I removed $request
and it appears to be working. Not sure why mine was giving me the error, guess it's possible it's been deprecated.
@alexabbott install that plugin:
https://github.com/airesvsg/acf-to-rest-api#get-acf-fields-recursively
here is a cleaner way for anyone else who finds this issue:
add_filter( 'acf/rest_api/page/get_fields', function( $data, $request, $response ) {
if ( $response instanceof WP_REST_Response ) {
$data = $response->get_data();
}
array_walk_recursive( $data, 'deepIncludeACFFields', array( 'rentals' ) );
return $data;
}, 10, 3 );
function deepIncludeACFFields( &$item, $key, $postTypes ) {
if ( isset( $item->post_type ) && in_array( $item->post_type, $postTypes ) ) {
$item = get_fields( $item->ID );
}
}
Hey, maybe I am missing something, but I am having trouble retrieving custom post ACF data within a post object field.
I have a custom post type call Rentals where I have set up ACF fields.
I can retrieve all of the acf data by getting /wp-json/wp/v2/rentals/{id}
When I create a page with an ACF Post Object field where I select a specific rental, I don't see any of the ACF data in the response. When I get /wp-json/wp/v2/pages/{id} I get this response:
How do I set it up so the ACF data is included within that Post Object field?