show_in_rest and register_post_meta(): custom post types and meta in the REST API and the block editor
A custom post type registered without show_in_rest has no REST endpoint and opens in the classic editor: the block editor loads and saves posts through the REST API. The same goes for meta: a field is part of the REST response, and editable in the block editor, only when it is registered with register_post_meta() and show_in_rest. Registration also gives the field a type, a default value, a sanitize callback and a permission check.
Problem: An event post type opens in the classic editor and has no /wp-json/wp/v2/events endpoint, and its date, location and “featured” fields are missing from REST responses.
Solution: Register the post type with 'show_in_rest' => true and custom-fields in supports, and every meta field with register_post_meta(): type, single, default, sanitize_callback, auth_callback and show_in_rest.
The post type
<?php
add_action( 'init', function () {
register_post_type( 'event', array(
'label' => 'Events',
'public' => true,
'has_archive' => true,
'show_in_rest' => true, // REST endpoint + block editor
'rest_base' => 'events', // /wp-json/wp/v2/events
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ),
) );
} );
Without custom-fields in supports, the REST API does not include the meta object for this post type, even for registered fields.
The meta fields
<?php
add_action( 'init', function () {
$can_edit = function () {
return current_user_can( 'edit_posts' );
};
register_post_meta( 'event', 'ha_event_date', array(
'type' => 'string',
'single' => true,
'default' => '',
'show_in_rest' => array( 'schema' => array( 'type' => 'string', 'format' => 'date' ) ),
'sanitize_callback' => function ( $value ) {
$date = DateTime::createFromFormat( 'Y-m-d', $value );
return ( $date && $date->format( 'Y-m-d' ) === $value ) ? $value : '';
},
'auth_callback' => $can_edit,
) );
register_post_meta( 'event', 'ha_event_location', array(
'type' => 'string',
'single' => true,
'show_in_rest' => true,
'sanitize_callback' => 'sanitize_text_field',
'auth_callback' => $can_edit,
) );
register_post_meta( 'event', 'ha_event_featured', array(
'type' => 'boolean',
'single' => true,
'default' => false,
'show_in_rest' => true,
'auth_callback' => $can_edit,
) );
} );
type and the schema make the REST API validate and convert values: ha_event_featured arrives as true/false in JSON, not as "1". default (WordPress 5.5+) is returned when the field is not set. The sanitize_callback runs on every save, also from PHP with update_post_meta(). The auth_callback decides who may write the field through the API; meta keys starting with _ are protected and cannot be written through REST at all without it.
Note that get_post_meta() in PHP still returns strings from the database ("1", ""); the type conversion happens in the REST API.
In the REST API
GET /wp-json/wp/v2/events/42?_fields=id,title,meta
POST /wp-json/wp/v2/events/42 {"meta": {"ha_event_date": "2021-08-20"}}
A wrong value, such as "20.08.2021" for the date, is answered with a 400 error that names the field. How to add computed fields that are not meta is shown in extending the REST API with register_rest_field().
In the block editor
Registered meta is part of the post in the editor’s data store, so a sidebar panel can read and change it; a complete example is in adding a panel to the Document sidebar.
NOTE: Register the meta for the right post type. register_post_meta( 'post', ... ) applies only to normal posts; for a field on all post types use register_meta( 'post', $key, array( 'object_subtype' => '' ... ) ), but think twice: a field registered for every post type appears in every REST response.