$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom field - How to return Meta data from the REST API?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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",

Post a comment

comment list (0)

  1. No comments so far