1. Home
  2. Wordpress
  3. Add meta data fields to rest api

Adds post meta data fields to returned JSON data in the REST API. You can use it for default post types or custom ones.

#wordpress#php#rest#api
/**
* Register post meta data in REST
* Use rest_api_init to register post's meta data
*/
add_action( 'rest_api_init', 'add_post_meta_to_rest' );

function add_post_meta_to_rest() {

    $post_type = 'post'; // You can use taxonomy or a custom post name.
    $rest_name = 'post-meta' // All meta fields will be stored under this REST attribute.

    register_rest_field( $post_type, $rest_name, array(
        'get_callback' => function ( $object ) { return get_post_meta( $object['id'] ); },
        'schema' => null,
        )
    );
}
copy
Full Wordpress cheatsheet