最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Expose a custom field of a custom post type to the REST API

programmeradmin3浏览0评论

I added a custom field to posts and exposed it to the REST API, which works for standard posts and appears in the APIs response. But does not work for a custom post type (projects) which is basically a standard post type just with a different name. The field is present in the project post type and I can edit and save it.

How can I register it to both post and the custom "project" post type? I guess I simply need to register it the same way as shown below. Can I register it to both types at once? This is how I register it for posts. The reference show that the $object_type is an array, so if I am not totally wrong, it should be possible, but how does it work?

/**********************************************
* Add featured video to REST API
***********************************************/
function add_featured_video_id() {
    register_rest_field(
    'post', 
    'featured_video_id',
    array(
        'get_callback'    => 'get_featured_video_id',
        'update_callback' => null,
        'schema'          => null,
         )
    );
}

add_action( 'rest_api_init', 'add_featured_video_id' );
    function get_featured_video_id( $object, $field_name, $request ) {
        $fv_id= get_post_meta(get_the_ID(), '_chrest_featured_video', true);
    return $fv_id;
}

I added a custom field to posts and exposed it to the REST API, which works for standard posts and appears in the APIs response. But does not work for a custom post type (projects) which is basically a standard post type just with a different name. The field is present in the project post type and I can edit and save it.

How can I register it to both post and the custom "project" post type? I guess I simply need to register it the same way as shown below. Can I register it to both types at once? This is how I register it for posts. The reference show that the $object_type is an array, so if I am not totally wrong, it should be possible, but how does it work?

/**********************************************
* Add featured video to REST API
***********************************************/
function add_featured_video_id() {
    register_rest_field(
    'post', 
    'featured_video_id',
    array(
        'get_callback'    => 'get_featured_video_id',
        'update_callback' => null,
        'schema'          => null,
         )
    );
}

add_action( 'rest_api_init', 'add_featured_video_id' );
    function get_featured_video_id( $object, $field_name, $request ) {
        $fv_id= get_post_meta(get_the_ID(), '_chrest_featured_video', true);
    return $fv_id;
}
Share Improve this question asked Jan 4, 2020 at 11:05 Carsten HCarsten H 1277 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Modify your code as below:

/**********************************************
* Add featured video to REST API
***********************************************/
function add_featured_video_id() {
    register_rest_field(
    array('post', 'project'), 'featured_video_id',
    array(
        'get_callback'    => 'get_featured_video_id',
        'update_callback' => null,
        'schema'          => null,
        )
    );
}

Visit here, for more details on method register_rest_field,

发布评论

评论列表(0)

  1. 暂无评论