Skip to content
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

Closed
amydws opened this issue Jun 13, 2018 · 13 comments
Closed

Custom fields and Featured Image from Relationship Field #251

amydws opened this issue Jun 13, 2018 · 13 comments

Comments

@amydws
Copy link

amydws commented Jun 13, 2018

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?

@amydws
Copy link
Author

amydws commented Jun 13, 2018

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.

@amydws
Copy link
Author

amydws commented Jun 13, 2018

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.
#153

This is solved.

@amydws amydws closed this as completed Jun 13, 2018
@posadallano
Copy link

Thanks @amydws very helpful.

@fredericpfisterer
Copy link

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 :)

@roryheaney
Copy link

@fredericpfisterer did you get this figured out?

@fredericpfisterer
Copy link

Not really :(

@SimonIMG
Copy link

SimonIMG commented Dec 5, 2019

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
);

@SimonIMG
Copy link

^^ 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 add_filter( 'acf/rest_api/{type}/get_fields', (using the wiildcard {type} instead of the post type slug post).

But my gatsby develop didn't like this. So we need to use an add_filter hook for each custom post type we want to query the featured images for.

/**
 * Add ability to query the featured image on posts added by the ACF relationship field.
 *
 * Reference: https://github.com/airesvsg/acf-to-rest-api/issues/251
 */
function get_featured_image_of_related( $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_media = $featured[0];
			}
		}
	}

	return $item;
}

/**
 * Add a custom hook for each post type where we want to query the related post featured image
 */
add_filter(
	'acf/rest_api/post/get_fields',
	get_featured_image_of_related,
	10,
	2
);

add_filter(
	'acf/rest_api/case_study/get_fields',
	get_featured_image_of_related,
	10,
	2
);

For more custom post types, simply add a new hook (ie. copy the add_filer block and update the path: 'acf/rest_api/[YOUR_POST_TYPE_SLUG]/get_fields',

@reslear
Copy link

reslear commented Sep 10, 2020

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);
}

@joehannouch
Copy link

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 ) {
if ( isset( $item['acf']['relationship'] ) ) {
foreach( $item['acf']['relationship'] as $key => $relation ) {
$relation_id = $relation->ID;
$featured = wp_get_attachment_image_src( get_post_thumbnail_id( $relation->ID ), 'full' );
if ( $featured ) $item['acf']['relationship'][$key]->featured_image = $featured[0];
}
}
return $item;
}, 10, 2 );

The relationship object still looks like this:

"relationship": [
{
"ID": 312,
"post_author": "1",
"post_date": "2021-01-14 13:58:45",
"post_date_gmt": "2021-01-14 13:58:45",
"post_content": "",
"post_title": "Original Bowl",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "open",
"ping_status": "closed",
"post_password": "",
"post_name": "acai-original-bowl-copy",
"to_ping": "",
"pinged": "",
"post_modified": "2021-01-14 13:58:49",
"post_modified_gmt": "2021-01-14 13:58:49",
"post_content_filtered": "",
"post_parent": 0,
"guid": "localhost/?post_type=product&p=312",
"menu_order": 0,
"post_type": "product",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw"
},

I tried the above-proposed code too but I get warnings at get_featured_image_of_related

Thank you!

@niran00
Copy link

niran00 commented Jun 1, 2021

@joehannouch
Where you able to find a solution?
I am trying to do the exact same thing, but instead of a featured image in the relationship object, I need to call in acf google maps field.

@joehannouch
Copy link

@joehannouch
Where you able to find a solution?
I am trying to do the exact same thing, but instead of a featured image in the relationship object, I need to call in acf google maps field.

No, i ended up calling both apis and building manually the json in the frontend.

@yannickninot
Copy link

You can juste do this
<?php foreach ( $featured_posts as $post ) { setup_postdata($post); if ( !empty(get_the_post_thumbnail($post->ID)) ) { echo get_the_post_thumbnail($post->ID); } } ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants