WordPress REST API (Version 2) の posts エンドポイントにカスタムフィールドの値を追加する

WordPress REST API (Version 2) で出力される JSON でカスタムフィールドで登録した値を取得したいけど Post Meta エンドポイント (http://example.com/wp-json/wp/v2/posts/<parent_id>/meta) はセキュリティの関係上、認証[*1]されたユーザにしか出力されないので Posts エンドポイント (http://example.com/wp-json/wp/v2/posts/<id>) でカスタムフィールドの値を出力するメモ。 [*1] Authentication | WP REST API v2 Documentation functions.php に以下を追加。 $response に出力される JSON のデータ(?)が入っているので rest_prepare_post をフックして値を書き換え。
function my_rest_prepare_post( $response, $post, $request ) {

$response-&gt;data['post_meta'] = array(
'hoge' =&gt; get_post_meta( $post-&gt;ID, 'key_hoge', true ),
'fuga' =&gt; get_post_meta( $post-&gt;ID, 'key_fuga', true ),
);

return $response;
}
add_filter( 'rest_prepare_post', 'my_rest_prepare_post', 10, 3 );
フィルターフックはプラグインディレクトリ内、 rest-api/lib/endpoints/class-wp-rest-posts-controller.php で定義されており post_type 毎に設定できるので pagesエンドポイント (http://example.com/wp-json/wp/v2/pages/<id>) に追加したいのであれば rest_prepare_page と。
/**
* Filter the post data for a response.
*
* The dynamic portion of the hook name, $this-&gt;post_type, refers to post_type of the post being
* prepared for the response.
*
* @param WP_REST_Response   $response   The response object.
* @param WP_Post            $post       Post object.
* @param WP_REST_Request    $request    Request object.
*/
return apply_filters( 'rest_prepare_' . $this-&gt;post_type, $response, $post, $request );
カスタマイズ方法を探していて WordPress › フォーラム » カスタムフィールドの内容をJSON REST APIで表示する を参考にしたのですがこの時は WordPress REST API (Version 1) で、 Version 2 になってフィルターフック名が変わっていたので躓いた…

参考サイト