Last active
March 31, 2022 18:36
-
-
Save dantetesta/9157df972bf43f486d3d46b345c5656b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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