0
votes

I use Advanced Custom Fields in a wordpress site. I created a custom field that is used in posts. Then I created a custom field (Relationship field) for the homepage so that I can choose posts from there. When I make an api call for my homepage, I see the acf that brings the post, but I can not see the acf that is used in the post. How can I expose all the fields to use them with my api? It should be something like

{
acf: {
relationship_field: [
{
id: 5,
post_content: "something...",
...
acf: {
...
},
...
}
]
}
}

I can not see that nested acf (I don't know if it should be that way actually, but you get the point). Thanks in advance.

1

1 Answers

0
votes

If I understood you correctly, you should call register_rest_field so the extra data is added to the response when fetching posts.


register_rest_field('post', '_embedded', array(
            'get_callback' => function ($post) {
                return [
                    'field_1' => get_field('field_xxxxxxxxxxxxx', $post['id']),
                    'field_2' => get_field('field_xxxxxxxxxxxxx', $post['id']),
                ];
            }
        ));