Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelotorres/2d567628a2614a7fec1c8f9365df78af to your computer and use it in GitHub Desktop.
Save marcelotorres/2d567628a2614a7fec1c8f9365df78af to your computer and use it in GitHub Desktop.
Muitas vezes necessitamos usar mais do que os valores padrão do post que o WordPress disponibiliza, tanto para facilitar o uso dos dados, quanto para diminuir o número de requisições... https://www.marcelotorresweb.com/adicionando-campos-customizados-na-resposta-da-rest-api-do-wordpress
<?php
/**
* Show custom fields in REST API
*/
// Callback function
function get_post_meta_for_api( $object ) {
$more_post_meta['featured_media_url'] = get_the_post_thumbnail_url( $object['id'], 'medium' );
return $more_post_meta;
}
//Register fields
function create_api_posts_meta_field() {
register_rest_field( 'post', 'custom_fields', array(
'get_callback' => 'get_post_meta_for_api',
'schema' => null,
)
);
}
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment