-
-
Notifications
You must be signed in to change notification settings - Fork 114
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
Custom fields and Featured Image from Relationship Field #251
Comments
I figured out from other Issues how to get the custom fields from the relationship objects #223 I'm still trying to figure out how to get the featured image from the relationship object. |
I added the thumbnail image URL to the Rest API for my custom post type, then used this thread to make the output match in the relationship post object output. This is solved. |
Thanks @amydws very helpful. |
Hello, I'm trying to retrieve ACF data and featured image from post object from a relationship field. The acf-to-rest-api-recursive plugin solves the first part but how can I add the featured image information ? I tried the links provided by @amydws but without success. Thanks in advance :) |
@fredericpfisterer did you get this figured out? |
Not really :( |
I managed to do this with the help of this post by Rory Heaney. First, I created a new relationship field called Related Posts, ensuring I ticked the Featured Image checkbox in Elements and set the Return Format to Post Object. Next, I installed the ACF to REST API recursive plugin so I was able to query the ACF fields of posts in the relationship field (not required to get the Featured Image, but I thought it was worth mentioning). Then I added this to my functions.php /**
* Add ability to query the featured image on posts added by the ACF relationship field.
*/
add_filter(
'acf/rest_api/post/get_fields',
function ( $item, $request ) {
if ( isset( $item['acf']['related_posts'] ) ) {
foreach ( $item['acf']['related_posts'] as $key => $relation ) {
$relation_id = $relation->ID;
$featured = wp_get_attachment_image_src( get_post_thumbnail_id( $relation->ID ), 'full' );
if ( $featured ) $item['acf']['related_posts'][ $key ]->featured_image = $featured[0];
}
}
return $item;
},
10,
2
); |
^^ Following on from what I added above. No need for (ACF to REST API recursive plugin)[https://github.com/airesvsg/acf-to-rest-api-recursive.git] at all. I've disabled it (it was causing Gatsby issues). I also needed to get the related image of custom posts. I found both (Rory Heaney)[https://fancysquares.blog/posts/wordpress-rest-api-add-featured-image-to-acf-relationship-post-object] and (another support reply)[https://wordpress.org/support/topic/show-the-field-label-values-in-acf-rest-api/#post-9602402] said to use But my
For more custom post types, simply add a new hook (ie. copy the |
or use deep for include Featured Image in Relationship Field function set_featured_image(&$item, $key){
if(isset($item->post_type) && isset($item->ID) ) {
$id = $item->ID;
$featured = wp_get_attachment_image_src( get_post_thumbnail_id($id), 'full' );
$item->featured_media = $featured ? $featured[0] : null;
}
}
function get_featured_image_of_related( $item, $request ) {
if( !empty($item) ) {
array_walk_recursive($item['acf'], 'set_featured_image');
return $item;
}
}
$featured_image_post_types = array('post', 'page');
foreach($featured_image_post_types as $item) {
add_filter("acf/rest_api/{$item}/get_fields", get_featured_image_of_related, 10, 2);
} |
Thanks for all the suggestions, i still didn't manage to make this work, I have a custom post type with a relationship field that I'm not able to expose the featured image (of the relationship items) in rest here's my code taken from the links you shared. add_filter( 'acf/rest_api/{type}/get_fields', function ( $item, $request ) { The relationship object still looks like this: "relationship": [ I tried the above-proposed code too but I get warnings at get_featured_image_of_related Thank you! |
@joehannouch |
No, i ended up calling both apis and building manually the json in the frontend. |
You can juste do this |
My custom post type has associated posts from a different post type via ACF Relationship Field. I see that the associated post's title and content is automatically shown under "acf": {} but that post's custom fields and featured image are not shown. Is there a way to get those fields as well?
The text was updated successfully, but these errors were encountered: