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

  • $response WP_REST_Response — The response object.
  • $post WP_Post — The post object.
  • $request WP_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

Group wizard

One session: this hook, then each related_hooks entry. Walk it and tell us what to change.

rest_prepare_post → rest_api_init

functions.php — rest_prepare_post
functions.php
PHP rest_prepare_post · Hook 1 of 2 · Step 1 of 6

Practice the rest_prepare_post WordPress filter hook in our interactive sandbox — a VS Code-style editor with a step-by-step rest_prepare_post tutorial, add_filter exercises, and instant feedback. No local WordPress install required.