-
-
Save rileypaulsen/9b4505cdd0ac88d5ef51 to your computer and use it in GitHub Desktop.
function wp_api_encode_acf($data,$post,$context){ | |
$data['meta'] = array_merge($data['meta'],get_fields($post['ID'])); | |
return $data; | |
} | |
if( function_exists('get_fields') ){ | |
add_filter('json_prepare_post', 'wp_api_encode_acf', 10, 3); | |
} |
I use the following when using v2 of https://github.com/WP-API/WP-API
function wp_rest_api_alter() {
register_api_field( 'post',
'fields',
array(
'get_callback' => function($data, $field, $request, $type){
if (function_exists('get_fields')) {
return get_fields($data['id']);
}
return [];
},
'update_callback' => null,
'schema' => null,
)
);
}
add_action( 'rest_api_init', 'wp_rest_api_alter');
@davidmaneuver Thank you so much !
This is working with REST API v2 for me: https://github.com/airesvsg/acf-to-rest-api
I have created a custom post type and it has meta too. the custom post name is before_after and it is using multi post thumbnail library two metas are before_image and after_image, how i can add these two metas to REST API using the following code:
function wp_api_encode_acf($data,$post,$context){
$field_name = "after_image";
$data['mymeta'] = get_post_meta( $object[ 'id' ], $field_name );
return $data;
}
add_filter('json_prepare_post', 'wp_api_encode_acf', 10, 3);
Thank you!
The given solution didn't work for me on WordPress 5.2 but this works.
/**
* Create REST field of ACF values for WordPress REST API
*/
function create_acf_fields_key_for_rest_api() {
register_rest_field(
[ 'post', 'page' ], // add post types here
'acf_fields', // name of the field
array(
'get_callback' => 'get_acf_fields_for_api',
'update_callback' => null,
'schema' => null,
)
);
}
add_action( 'rest_api_init', 'create_acf_fields_key_for_rest_api' );
/**
* Get ACF fields of the current post for the REST API field
*
* @param array $object post object array
* @return array acf fields related to current post
*/
function get_acf_fields_for_api( $object ) {
$data = get_fields( get_the_ID() );
return $data ?: false;
}
@mehmoodak: Thank you :)
@albertoavv The REST route you are giving isn't correct. I didn't work with ACF REST API so please read their docs carefully. By the way I also thinks that in your path wp/wp5/wp-json
should be changed to wp/wp-json
Thank you for your response, if what happens is that w5 is the name of the project or the root, but thanks for your response I will continue looking for a solution,
Regards
@mehmoodak thnx for your solution. Do you have any suggestion to specify only which ACF fields do you want to retrieve or not ?
great contribution @rileypaulsen! thanks. it works
Thanks @mehmoodak !
Thank you so much @mehmoodak !
Hello, how do you use it ?
I tried :
http://localhost/wp-json/wp/v2/posts/16/
to retrieve all the post infos and but the meta section goes :I can't see my custom fields, and when I try this link it returns 403 has I'm not logged, but I want this data to be public read only.
Thanks