I want to add custom fields in my post so i am using ACF for this but the rest api is not showing any acf value so how can i use acf with frontity
Welcome to the community forum. We’re delighted that you’re working with Frontity. We’d love to know more about your experience of working with Frontity.
ACF doesn’t expose the fields in the REST API. You need an additional plugin to expose ACF fields. We’ve successfully used this one:
Hope this helps. Do let us know how you get on.
If you don’t want to install a plugin you can add some code similar to the following to your WordPress theme’s functions.php
file.
// add custom fields to REST API
function add_acf_fields_to_rest( $data, $post, $request ) {
$_data = $data->data;
$_data['myfield'] = get_field('myfield', $post->ID);
/* add more fields here if necessary */
$data->data = $_data;
return $data;
}
add_filter( 'rest_prepare_post', 'add_acf_fields_to_rest', 10, 3 );
Obviously replace myfield
above with the name of your ACF field. If you’re working with a custom post type then the filter hook would be changed to the slug of your CPT. So for example, if you had a book
custom post type then the filter hook would be rest_prepare_book
instead of rest_prepare_post
.
Thanks a lot for solving my problem. This plugin is just a life saver.