Skip to content

Instantly share code, notes, and snippets.

@MelMacaluso
Created June 4, 2019 22:54
Show Gist options
  • Save MelMacaluso/6c4cb3db5ac87894f66a456ab8615f10 to your computer and use it in GitHub Desktop.
Save MelMacaluso/6c4cb3db5ac87894f66a456ab8615f10 to your computer and use it in GitHub Desktop.
Automatically expose all the ACF fields to the Wordpress REST API in Pages and in your custom post types.
<?php
function create_ACF_meta_in_REST() {
$postypes_to_exclude = ['acf-field-group','acf-field'];
$extra_postypes_to_include = ["page"];
$post_types = array_diff(get_post_types(["_builtin" => false], 'names'),$postypes_to_exclude);
array_push($post_types, $extra_postypes_to_include);
foreach ($post_types as $post_type) {
register_rest_field( $post_type, 'ACF', [
'get_callback' => 'expose_ACF_fields',
'schema' => null,
]
);
}
}
function expose_ACF_fields( $object ) {
$ID = $object['id'];
return get_fields($ID);
}
add_action( 'rest_api_init', 'create_ACF_meta_in_REST' );
@abetoots
Copy link

Hey thanks for sharing this! I was wondering how you would implement this as editable fields? I would like to be able to update them through the api.

@MelMacaluso
Copy link
Author

Hey thanks for sharing this! I was wondering how you would implement this as editable fields? I would like to be able to update them through the api.

Hi! Can you expand on what you would need? Ideally what this script is for is to dynamically have all the fields you created in ACF exposed to the wp-json api so that you can query them with your js or js framework of choice.

@abetoots
Copy link

I would like to be able to update the fields in the 'update_callback' of register_rest_field() . Since this script exposes all fields, I was wondering if there was also a 'catch all' snippet for updating the fields.

@nimmolo
Copy link

nimmolo commented Jul 30, 2019

THANKS very much for sharing this gist.
+1 for @abetoots, that's also what i'm trying to do, expose the fields not only for viewing but also for updating (via PUT request)

@medusiora
Copy link

Many thanks for sharing this, It works well but not for the post relations field.
It's return normal post object.
Do you have some solution for this ?

@marty-mcgee
Copy link

marty-mcgee commented Nov 18, 2020

I appreciate you sharing this approach! However, I am getting a "null" return in the REST field
"ACF": null
Any suggestions? Thanks so much!

@johnsfuller
Copy link

Can someone help me out here? Why is ['acf-field-group','acf-field'] the value for $postypes_to_exclude = ['acf-field-group','acf-field']?

@MelMacaluso
Copy link
Author

Can someone help me out here? Why is ['acf-field-group','acf-field'] the value for $postypes_to_exclude = ['acf-field-group','acf-field']?

Hi there, because is the array of post_types to exclude that can be expanded as needed, if you don't do that (comment it out or make it an empty array) check what happens :)

@felipelenin
Copy link

Hi, i need help because i have two fields in my post, but my response it empty or null

[ { "id": 1, "date": "2022-06-28T13:10:06", "date_gmt": "2022-06-28T13:10:06", "guid": { "rendered": "http://localhost/mensageria/?p=1" }, "modified": "2022-06-28T21:02:01", "modified_gmt": "2022-06-28T21:02:01", "slug": "hello-world", "status": "publish", "type": "post", "link": "http://localhost/mensageria/2022/06/28/hello-world/", "title": { "rendered": "Hello world!" }, "content": { "rendered": "\n<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>\n", "protected": false }, "excerpt": { "rendered": "Welcome to WordPress. This is your first post. Edit or delete it, then start writing!", "protected": false }, "author": 1, "featured_media": 0, "comment_status": "open", "ping_status": "open", "sticky": false, "template": "", "format": "standard", "meta": [], "categories": [ 1 ], "tags": [], "acf": [], "_links": { "self": [ { "href": "http://localhost/mensageria/wp-json/wp/v2/posts/1" } ], "collection": [ { "href": "http://localhost/mensageria/wp-json/wp/v2/posts" } ], "about": [ { "href": "http://localhost/mensageria/wp-json/wp/v2/types/post" } ], "author": [ { "embeddable": true, "href": "http://localhost/mensageria/wp-json/wp/v2/users/1" } ], "replies": [ { "embeddable": true, "href": "http://localhost/mensageria/wp-json/wp/v2/comments?post=1" } ], "version-history": [ { "count": 4, "href": "http://localhost/mensageria/wp-json/wp/v2/posts/1/revisions" } ], "predecessor-version": [ { "id": 13, "href": "http://localhost/mensageria/wp-json/wp/v2/posts/1/revisions/13" } ], "wp:attachment": [ { "href": "http://localhost/mensageria/wp-json/wp/v2/media?parent=1" } ], "wp:term": [ { "taxonomy": "category", "embeddable": true, "href": "http://localhost/mensageria/wp-json/wp/v2/categories?post=1" }, { "taxonomy": "post_tag", "embeddable": true, "href": "http://localhost/mensageria/wp-json/wp/v2/tags?post=1" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } } ]

@felipelenin
Copy link

Can someone help me out here? Why is ['acf-field-group','acf-field'] the value for $postypes_to_exclude = ['acf-field-group','acf-field']?

Hi there, because is the array of post_types to exclude that can be expanded as needed, if you don't do that (comment it out or make it an empty array) check what happens :)

Hi, my code is the same problem.
But i forgete this field "Show in REST API" in settings you need set to "YES".

So is done, thanks

@easaw
Copy link

easaw commented Oct 6, 2022

I was wondering if there was a way to filter this to only show some of the fields (per post type)?

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment