I am building a custom WordPress theme and using Advanced Custom Fields (ACF) to manage dynamic content. I want to fetch and display ACF fields via the REST API, but I’m not getting the expected data in my API response.
add_action('rest_api_init', function() {
register_rest_field('post', 'my_custom_field', array(
'get_callback' => function($post_arr) {
return get_field('my_custom_field', $post_arr['id']);
},
'schema' => null,
));
});
I expected the API to return the value of my_custom_field for each post, but the field is either missing or empty in the API response.
WordPress version: 6.x
ACF version: 6.x
Custom theme: Yes
Using standard WP REST API endpoints: /wp-json/wp/v2/posts