1

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

1
  • 1
    Try approach mentioned here Commented Sep 25 at 7:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.