对我来说,最简单的解决方案是在JSON响应中创建额外的字段,并用选定的post meta填充它:
function create_api_posts_meta_field() {
// "tribe_venue" is your post type name,
// "protected-fields" is a name for new JSON field
register_rest_field( \'tribe_venue\', \'protected-fields\', [
\'get_callback\' => \'get_post_meta_for_api\',
\'schema\' => null,
] );
}
add_action( \'rest_api_init\', \'create_api_posts_meta_field\' );
/**
* Callback function to populate our JSON field
*/
function get_post_meta_for_api( $object ) {
$meta = get_post_meta( $object[\'id\'] );
return [
\'venue\' => $meta[\'_VenueVenue\'] ?: \'\',
\'address\' => $meta[\'_VenueAddress\'] ?: \'\',
\'city\' => $meta[\'_VenueCity\'] ?: \'\',
];
}
您应该能够在
/wp-json/wp/v2/tribe_venue/
和
/wp-json/wp/v2/tribe_venue/{POST_ID}