custom field - How to return Meta data from the REST API?

admin2025-06-02  1

I added meta box City and Location to my post, but when I calling the endpoint (by WP Rest API) /wp-json/wp/v2/<post_type>/<post_id> it's not returning my meta data.

I tried to use register_meta like this, but still not working:

register_meta('my_post_type', 'city', [
    'type' => 'string',
    'description' => 'Cidade',
    'single' => true,
    'show_in_rest' => true
]);

What should I do to return it?

I added meta box City and Location to my post, but when I calling the endpoint (by WP Rest API) /wp-json/wp/v2/<post_type>/<post_id> it's not returning my meta data.

I tried to use register_meta like this, but still not working:

register_meta('my_post_type', 'city', [
    'type' => 'string',
    'description' => 'Cidade',
    'single' => true,
    'show_in_rest' => true
]);

What should I do to return it?

Share Improve this question edited Mar 18, 2019 at 14:09 Sally CJ 40.3k2 gold badges29 silver badges50 bronze badges asked Mar 16, 2019 at 22:25 Lai32290Lai32290 3512 gold badges4 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 14

The first parameter passed to register_meta() is always post for posts, Pages (post type of page) and custom post types.

However, the REST API Handbook says that:

Prior WordPress 4.9.8, meta fields set to show_in_rest using register_meta are registered for all objects of a given type. If one custom post type shows a meta field, all custom post types will show that meta field. As of WordPress 4.9.8 it’s possible to use register_meta with the object_subtype argument that allows one to reduce the usage of the meta key to a particular post type.

So this should work:

register_meta('post', 'city', [
    'object_subtype' => 'my_post_type', // Limit to a post type.
    'type'           => 'string',
    'description'    => 'Cidade',
    'single'         => true,
    'show_in_rest'   => true,
]);

But then, the REST API Handbook also says:

Note that for meta fields registered on custom post types, the post type must have custom-fields support. Otherwise the meta fields will not appear in the REST API.

So make certain that your post type has support for custom-fields:

register_post_type(
    'my_post_type',
    array(
        'supports' => array( 'custom-fields', ... ), // custom-fields is required
        ...
    )
);

Alternate Solution

This one uses register_rest_field() and you can easily use my_post_type in the code. But this is of course a simplified example and you should check the handbook for more information:

add_action( 'rest_api_init', function () {
    register_rest_field( 'my_post_type', 'city', array(
        'get_callback' => function( $post_arr ) {
            return get_post_meta( $post_arr['id'], 'city', true );
        },
    ) );
} );

Also, the metadata will not be in meta, but instead in the top-level just as the meta property. Sample response body/string: (just part of the full response)

  • With register_meta(): ,"meta":{"city":"London"},

  • With register_rest_field(): ,"meta":[],"city":"London",

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748796617a313794.html

最新回复(0)