WordPress REST API – Custom field not added to pages

You may need to flush the REST API cache in order to see the added field. You can do this by adding the following code to your plugin:

This code will flush the REST API cache whenever a post (including a page) is saved or updated, so that the changes to the registered field will appear immediately.

If the field still does not appear after flushing the cache, you may want to check that the REST API request you’re making is for a single page object and not a collection. The field will only be returned when you make a GET request for a single page, for example: wp-json/wp/v2/pages/123.

function flush_rest_api_cache() {
    wp_cache_flush();
}
add_action( 'save_post', 'flush_rest_api_cache' );