像bueltge提到的那样,您需要注册自定义元字段并向api公开。最近,当我试图为我们的用户获取自定义元字段时,我遇到了这个问题。我认为我的解决方案将适用于您,如果您将其用于帖子和用户(请注意Register new field
第节)。法典文件也很有帮助。
// 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 );
}