Get Custom Fields from wp-json response

1.4k views Asked by At

The response for all posts in wp-json does not include a couple of Custom Fields that I created in my posts. I call it this way: /wp-json/posts

How can I make sure the JSON response also contains my Post Custom Fields?

1

There are 1 answers

3
Gavin Simpson On BEST ANSWER

Check out this link.

Turns out this is a problem at the mo in Wordpress, but the link has a suggestion fix (amongs others) as such :

function json_api_prepare_post( $post_response, $post, $context ) {

  $field = get_field( "field_name", $post['ID'] );

  $post_response['field_name'] = $field;

  return $post_response;
}
add_filter( 'json_prepare_post', 'json_api_prepare_post', 10, 3 );

You will however have to go through the full post as the above link, s it turns out a lot of people are having this issue, but a number of them got the issue resolved with all the suggestions in the post.

It also appears this question has been asked before. refer to this question.