2

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?

denicio
  • 562
  • 1
  • 7
  • 22

1 Answers1

2

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.

Community
  • 1
  • 1
Gavin Simpson
  • 2,766
  • 3
  • 30
  • 39
  • Thank you for your comment, but I am using "Custom Fields" on a wordpress post, and not "Advanced Custom Fields" wordpress plugin. Hence the "get_field" function is undefined in my case. – denicio Jan 28 '15 at 09:08
  • Just guessing, but instead of the get_field line try "$custom=get_post_custom($post_id);", and then you should be able to access the $custom array eg $custom['field_name']. – Gavin Simpson Jan 28 '15 at 11:14
  • Can you answer how one can modify the wp-json response for a specific post only See: https://stackoverflow.com/questions/49330395/how-to-modify-title-in-wp-json-response-but-only-for-a-specific-post – James Hawkins Mar 18 '18 at 23:43