如何将POST中定义的自定义字段添加到wordpress中的RESTAPI响应中

如何将POST中定义的自定义字段添加到wordpress中的RESTAPI响应中,wordpress,wordpress-rest-api,Wordpress,Wordpress Rest Api,我不想使用任何插件,因为这两个都是wordpress的核心功能(自定义字段和RESTAPI)。以下是自定义字段的文档,以供参考: 以下是我的wordpress安装的屏幕截图: 以下是post的API响应当前的样子: { "_links": { "about": [ { "href": "http://example.com/wp-json/wp/v2/types/post" }

我不想使用任何插件,因为这两个都是wordpress的核心功能(自定义字段和RESTAPI)。以下是自定义字段的文档,以供参考:

以下是我的wordpress安装的屏幕截图:

以下是post的API响应当前的样子:

{
    "_links": {
        "about": [
            {
                "href": "http://example.com/wp-json/wp/v2/types/post"
            }
        ],
        "author": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/users/1"
            }
        ],
        "collection": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts"
            }
        ],
        "curies": [
            {
                "href": "https://api.w.org/{rel}",
                "name": "wp",
                "templated": true
            }
        ],
        "replies": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/comments?post=21"
            }
        ],
        "self": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts/21"
            }
        ],
        "version-history": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts/21/revisions"
            }
        ],
        "wp:attachment": [
            {
                "href": "http://example.com/wp-json/wp/v2/media?parent=21"
            }
        ],
        "wp:featuredmedia": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/media/23"
            }
        ],
        "wp:term": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/categories?post=21",
                "taxonomy": "category"
            },
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/tags?post=21",
                "taxonomy": "post_tag"
            }
        ]
    },
    "author": 1,
    "categories": [
        5,
        4
    ],
    "comment_status": "open",
    "content": {
        "protected": false,
        "rendered": ""
    },
    "date": "2017-05-14T15:25:33",
    "date_gmt": "2017-05-14T15:25:33",
    "excerpt": {
        "protected": false,
        "rendered": ""
    },
    "featured_media": 23,
    "format": "standard",
    "guid": {
        "rendered": "http://example.com/?p=21"
    },
    "id": 21,
    "link": "http://example.com/2017/05/14/post/",
    "meta": [],
    "modified": "2017-05-15T18:17:34",
    "modified_gmt": "2017-05-15T18:17:34",
    "ping_status": "open",
    "slug": "",
    "sticky": false,
    "tags": [],
    "template": "",
    "title": {
        "rendered": ""
    },
    "type": "post"
}
如果可能相关,以下是我的活动插件:

首先,您需要在WP REST API JSON响应中添加自定义端点

add_action( 'rest_api_init', 'add_custom_fields' );
function add_custom_fields() {
register_rest_field(
'post', 
'custom_fields', //New Field Name in JSON RESPONSEs
array(
    'get_callback'    => 'get_custom_fields', // custom function name 
    'update_callback' => null,
    'schema'          => null,
     )
);
}
然后定义函数以

在当地现场测试


只需将其添加到CMS中,就可以在WP REST API中使用。

将此插件用于此解决方案:
因为它允许您通过此处列出的新端点将高级自定义字段公开到它们自己的JSON响应中。然后,您可以
forkJoin
两个观察值(如果使用角度),并根据您的目的使用组合响应。

我发现该插件非常有用。

谢谢,这很有意义。这段代码应该放在哪里?在你的主题函数中,或者如果你在你的插件函数中使用插件,对不起,但是我如何给“自定义_字段”添加一个值呢?好答案。这是修改响应(register\u rest\u字段),而不是添加自定义端点(register\u rest\u route)。@PaulRoub我根据请求调整了答案
function get_custom_fields( $object, $field_name, $request ) {
//your code goes here
return $customfieldvalue;
}