-
-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get ACF post object field with ACF fields #109
Comments
Hi @josephdonnelly, Edit: Get ACF Fields Recursively: |
Thanks for the extremely quick response! I used that snippet, and it works great: add_filter( 'acf/rest_api/page/get_fields', function( $data, $request, $response ) {
if ( $response instanceof WP_REST_Response ) {
$data = $response->get_data();
}
if ( isset($data['acf']) && isset( $data['acf']['rental'] ) ) {
$data['acf']['rental'] = get_fields($data['acf']['rental']->ID);
}
return $data;
}, 10, 3); However, is there a way to set it up so that any time a Rental posts is being used it displays the acf data? I am going to be using that Rental post type within several custom fields (repeaters/flexcontent/etc) and it will get tedius having to check for those fields manually. Thanks again for the quick response |
Also worth noting, with the above snippet I was receiving this error using version 3 of the plugin: Error goes away when switching back to v2 |
At this time manually, but you can do a recursive function to check. Yes, in third version the argument $response not exists. add_filter( 'acf/rest_api/page/get_fields', function( $data, $request ) {
...
}, 10, 2 ); Arguments of add_filter( $tag, $function_to_add, $priority, $accepted_args ); The Thanks |
Thanks again. I got it working with a recursive function 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, 'deepIncludeRentalFields' );
return $data;
}, 10, 3);
function deepIncludeRentalFields( &$item, $key ) {
if ( $key == 'rental' ) {
$item = get_fields( $item->ID );
}
} |
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 );
}
} |
@josephdonnelly I am dealing with this same thing... I think. Can you help provide a better explanation of how you fixed this? I have a custom post type I am pulling info for via the API. On each post there is a Relationship field where you can select other posts from the same post type. The JSON shows the objects from the relationship but NOT any of their custom fields. I want to pull field data from the relationship objects. The snippet above you listed doesn't appear to be working for me. Any help would be awesome! Thanks! |
Hi @travis-zookacreative, 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' );
}
}
} |
hey @airesvsg thanks for sharing! Unfortunately that throws an error and doesn't load. Any ideas? I am using V2 at the moment. My custom post type is "product" so my first line looks like the following.
|
@travis-zookacreative , Try this one out. It will run the same recursive lookup if the item is an array. I just ran into this and solved it this way add_filter( 'acf/rest_api/product/get_fields', function( $data, $request, $response ) {
if ( $response instanceof WP_REST_Response ) {
$data = $response->get_data();
}
array_walk_recursive($data, 'deepIncludeACFFields', array('product'));
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);
}
// Add this portion here to look up arrays within the array
if(is_array($item)) {
array_walk_recursive($item, 'deepIncludeACFFields', $postTypes);
}
} |
@josephdonnelly I saw you posted something similar earlier up the chain. I've messed around with both but they are throwing a fatal error of "Too few arguments to function {closure}()". I also saw you had this same issue, how did you resolve? |
Can you post the full code you're using? |
add_filter( 'acf/rest_api/product/get_fields', function( $data, $request, $response ) {
if ( $response instanceof WP_REST_Response ) {
$data = $response->get_data();
}
array_walk_recursive($data, 'deepIncludeACFFields', array('product'));
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);
}
// Add this portion here to look up arrays within the array
if(is_array($item)) {
array_walk_recursive($item, 'deepIncludeACFFields', $postTypes);
}
} I changed my local server from PHP 7 to 5.6 and that got rid of the too few arguments error. Now instead it just times out hahaha |
Ah, so the timeout likely is because you're going in an infinite loop. You get acf fields for a product, which gets acf fields for a product, which gets acf fields for a product... and so on. You will have to add some checks into the deepincludeACFFields to prevent the infinite loop. I'm afraid I don't know enough about PHP or your specific situation to help. Best of luck though, |
ahhh that would make sense! Thanks man! Much Appreciated! |
Gents, got this working! Great news! However do you know of any ways to ALSO get WP data from the relationship posts? I would also like to grab the slug, and featured img, etc. Not just the ACF fields. Code I got to work is below. add_filter( 'acf/rest_api/product/get_fields', function( $data, $request, $response ) {
if ( $response instanceof WP_REST_Response ) {
$data = $response->get_data();
}
array_walk_recursive($data, 'deepIncludeACFFields', array('product'));
return $data;
}, 10, 3);
function deepIncludeACFFields(&$item, $key, $postTypes, $level=0, $post) {
if( isset($item->post_type) && in_array($item->post_type, $postTypes) ) {
$item = get_fields($item->ID);
}
} |
Great information. |
Hi, Fixed - see end of comment I've been able to use one of the code snippets above:
To successfully get the ACF fields from a page or post-type. However, I am struggling to get the fields from a custom options page. I've tried replacing:
With
but I haven't had any success yet. How should I go about getting relational fields from options pages? The structure that I am working with looks like this:
I'm looking to get the ACF fields from Thanks! Fix
Also turns out that I was saying |
hi everyone. for the life of me i cant seem to get this to work! I have tried both post object as well as post relationship. none of these snippets are returning acf data in the returned post object... im using the default wp-api. any help is much appreciated! :D |
Hi @airesvsg - I am using the above snippets (specifically @travis-zookacreative to return acf post objects in a repeater that is in a custom post type. the endpoint URL looks like this: When I apply deepIncludeACFFields function & filter I get the acf data but i loose the normal post data, I need to be able to pull the title of the post and the slug for a permalink. After reading through the various tickets and trying some different things still cant seem to get both - so is this possible or is it one or the other - and I should include things like title and slug as custom fields. Thanks! |
Hey @airesvsg. Ok, so its been a while since I worked on this project but here is my code at the moment. I'll do my best to remember what it's doing. So essentially I decided it best to get just the acf fields I want if they exist. So for each custom type that has the repeater field
|
Setting the $item to the full ACF field overwrites all the default post object data. I instead looped through the fields and append them to the item in order to presever the default data: function deepIncludeACFFields( &$item, $key, $postTypes, $level=0, $post ) {
if ( isset( $item->post_type ) && in_array( $item->post_type, $postTypes ) ) {
$acf_fields = get_fields( $item->ID );
foreach( $acf_fields as $key => $value ) {
$item->{$key} = $value;
}
}
} |
Instead of defining it for every possible post type. I added a /**
* 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 Also, I was getting an error with |
function( $data, $request, $response ) breaks in PHP 7.x, I haven't done too much digging as to why. If anyone has an answer that'd be great. Still works in PHP 5.x |
can you provide simple instructions for where to add this filter? what file? |
@alexabbott install that plugin: |
@airesvsg thank you! works like a charm |
Hi @airesvsg I'm trying to implement on the site that I'm developing. I've installed your recursive plugin but the response still doesn't have the acf fields included. I'm on v3 of the acf-to-rest-api plugin. https://nationalgridpurpose.com/wp-json/wp/v2/actionbox/10084 Any advice please? |
@mattprudente Did you figure this out? I've installed the plugin but still no acf fields. |
Did anyone else figure this out, for adding fields to the base post object as detailed above? not just the ACF fields, but things like Categories or the Featured Image. |
How could i enrich this "acf": { |
this plugins get api one load is properly but when i edit act content so this update content is not show in my react project and my costume api is working fine |
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?
The text was updated successfully, but these errors were encountered: