Filter Hook since 4.7.0
rest_prepare_post
Filters the REST API response for a single post before it's sent
Description
rest_prepare_post lets you add or remove fields on the /wp/v2/posts response without touching register_rest_field — useful for last-mile shaping of what the API returns.
When it runs
Inside the REST posts controller, after the response object is built from the post data.
Signature
apply_filters( 'rest_prepare_post', WP_REST_Response $response, WP_Post $post, WP_REST_Request $request );Parameters
$responseWP_REST_Response — The response object.$postWP_Post — The post object.$requestWP_REST_Request — The request object.
Examples
Basic
add_filter( 'rest_prepare_post', function( $response, $post ) {
$response->data['reading_time'] = ceil( str_word_count( $post->post_content ) / 200 );
return $response;
}, 10, 2 );Add a computed reading-time field to the REST response.
Common Use Cases
- Add computed/derived fields to the REST response
- Strip sensitive fields from public API output
- Reshape data for a headless front-end
Common mistakes
- Adding fields here instead of via register_rest_field() when you also need them documented in the schema for REST API consumers
Related hooks
FAQ
rest_prepare_post vs register_rest_field — which should I use?
register_rest_field is the documented way to add a field with schema support; rest_prepare_post is a lower-level filter for quick tweaks or removing/reshaping existing fields.
Source: wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php