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

Relation add acf fields? #223

Closed
cannap opened this issue Mar 26, 2018 · 13 comments
Closed

Relation add acf fields? #223

cannap opened this issue Mar 26, 2018 · 13 comments

Comments

@cannap
Copy link

cannap commented Mar 26, 2018

hi as example
i have a

relation to a custom post type

now i need some acf fields in the relations output i got via acf but i dont know how :/

i use //xxxx.com/wp-json/wp/v2/pages?slug=arbeiten
unbenannt

@dmnsgn
Copy link

dmnsgn commented Apr 3, 2018

Hi @airesvsg and @rainboxx ,

I see you have been discussing the topic of adding acf fields to Relationship objects in the following PR #95 some time ago.

Is it implemented somehow or is there any workaround specifically with the ACF Pro v5?

Thanks

@rainboxx
Copy link

rainboxx commented Apr 3, 2018

I'm no longer on the specific project where we used this plugin so can't really help here to tell how we implemented our case. I remember that we have changed how we use the API back then so we no longer had this requirement.

My PR was not implemented since I couldn't get it running with the Pro v5. I tried following-up but since priority and consumption changed I couldn't finish this.

@airesvsg
Copy link
Owner

airesvsg commented Apr 3, 2018

Hi @dmnsgn,

I didn't the implementation, but you can use that solution: #109 (comment)

Thanks

@dmnsgn
Copy link

dmnsgn commented Apr 3, 2018

Thanks for the pointers. Since I am using the Custom Post Type UI plugin, I ended up getting all the different post types (including customs) and add_filter for each:

<?php
$post_types = array_merge(get_post_types(), cptui_get_post_type_slugs());

foreach ($post_types as $type) {
	add_filter(
		'acf/rest_api/' . $type . '/get_fields',
		function ($data, $response) use ($post_types) {
			if ($response instanceof WP_REST_Response) {
				$data = $response->get_data();
			}

			array_walk_recursive($data, 'get_fields_recursive', $post_types);

			return $data;
		},
		10,
		3
	);
}

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

@cannap that should work for you.

@airesvsg
Copy link
Owner

airesvsg commented Apr 3, 2018

Hi @dmnsgn,

Please, test it:
https://github.com/airesvsg/acf-to-rest-api-recursive

Thanks

@cannap
Copy link
Author

cannap commented Apr 4, 2018

thanks all it works! now i can save some duplicated content and time :D

@dmnsgn
Copy link

dmnsgn commented Apr 4, 2018

@airesvsg I can't manage to make it work with the recursive plugin but it might be because my Custom Post Type are handled via the CPTUI plugin and you need to use this function to retrieve them: cptui_get_post_type_slugs()

@airesvsg
Copy link
Owner

airesvsg commented Apr 4, 2018

Hi @dmnsgn,

Please, try it:

add_filter( 'acf/rest_api/recursive/types', function( $types ) {
  $slugs = (array) cptui_get_post_type_slugs();

  $types += array_combine( $slugs, $slugs );

  return $types;
} );

Thanks

@dmnsgn
Copy link

dmnsgn commented Apr 4, 2018

Yep, that works fine!

@airesvsg airesvsg closed this as completed Apr 6, 2018
@jaredpalmer
Copy link

Worked for me too!

@kgrosvenor
Copy link

Hi guys, how are you guys not getting a response like this?

"acf": {
"audio_file_url": false,
"summary": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. ",
"video_file_url": "http://techslides.com/demos/sample-videos/small.mp4",
"attached_resources": [
{
"help": [
{
"ID": 1,
"post_author": "1",
"post_date": "2018-10-09 18:50:19",
"post_date_gmt": "2018-10-09 18:50:19",
"post_content": "Welcome to WordPress. This is your first post. Edit or delete it, then start writing!",
"post_title": "Hello world!",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "open",
"ping_status": "open",
"post_password": "",
"post_name": "hello-world",
"to_ping": "",
"pinged": "",
"post_modified": "2018-10-09 18:50:19",
"post_modified_gmt": "2018-10-09 18:50:19",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http://104.248.160.79/?p=1",
"menu_order": 0,
"post_type": "post",
"post_mime_type": "",
"comment_count": "1",
"filter": "raw"
}
]
}
]
},

@kgrosvenor
Copy link

See the nested object? how to i stop that

@oggzr
Copy link

oggzr commented Oct 15, 2020

How do you prevent this from turning into a infinity loop? For example if one of the relation posts are the same as the parent post.

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

7 participants