Like bueltge alluded, you need to register custom meta fields and expose to to the api. I recently had this problem when I was trying to get a custom meta field for our users. I think my solution will work for you if you adapt it for posts vs users (pay attention to the Register new field
section). The codex docs were very helpful, too.
// Extends user profiles with building_id field
function fb_add_custom_user_profile_fields( $user ) {
?>
<table class="form-table">
<tr>
<th>
<label for="building_id"><?php _e('Building ID', 'your_textdomain'); ?>
</label></th>
<td>
<input type="text" name="building_id" id="building_id" value="<?php echo esc_attr( get_the_author_meta( 'building_id', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e('Please enter your building_id.', 'your_textdomain'); ?></span>
</td>
</tr>
</table>
<?php }
function fb_save_custom_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return FALSE;
update_usermeta( $user_id, 'building_id', $_POST['building_id'] );
}
add_action( 'show_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'edit_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields' );
// Register new field (building _id) and expose it to WP REST API
// See docs: https://developer.wordpress.org/reference/functions/register_rest_field/
add_action( 'rest_api_init', 'create_api_users_meta_field' );
function create_api_users_meta_field() {
// Codex format example: register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'user', 'buidling_id', array(
'get_callback' => 'get_user_meta_for_api',
'schema' => null,
)
);
}
function get_user_meta_for_api( $object ) {
//get the id of the user object array
$user_id = $object['id'];
//return the user meta
return get_user_meta( $user_id );
}