Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dantetesta/9157df972bf43f486d3d46b345c5656b to your computer and use it in GitHub Desktop.
Save dantetesta/9157df972bf43f486d3d46b345c5656b to your computer and use it in GitHub Desktop.
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'posty_tyepe_name', 'metafields', array(
'get_callback' => 'get_post_meta_for_api',
'schema' => null,
)
);
}
function get_post_meta_for_api( $object ) {
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
return get_post_meta( $post_id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment