Skip to content

Instantly share code, notes, and snippets.

@Ashkanph
Last active January 5, 2019 05:29
Show Gist options
  • Save Ashkanph/0f6413cf224bea4c2fbaa27b2da5fe4d to your computer and use it in GitHub Desktop.
Save Ashkanph/0f6413cf224bea4c2fbaa27b2da5fe4d to your computer and use it in GitHub Desktop.
Getting wordpress custom fields in response of wordpress API
  • "access" is a custom field of the posts. Add this to the functions.php:
function get_post_meta_cb($object, $field_name, $request){
        return get_post_meta($object['id'], $field_name, true); 
}
function update_post_meta_cb($value, $object, $field_name){
  return update_post_meta($object['id'], $field_name, $value); 
}
add_action('rest_api_init', function(){
  register_rest_field('post', 'access', 
    array(
    'get_callback' => 'get_post_meta_cb', 
    'update_callback' => 'update_post_meta_cb', 
    'schema' => null
    )
  ); 
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment