如何将帖子中定义的自定义字段添加到 wordpress 中的其余 API 响应
How do you add custom fields defined in posts to the rest API responses in wordpress
我想在不使用任何插件的情况下执行此操作,因为这些都是核心 wordpress 功能(自定义字段和 REST API)。以下是自定义字段的文档供参考:
https://codex.wordpress.org/Using_Custom_Fields
这是我的 wordpress 安装截图:
这是 API 对 post 的响应目前的样子:
{
"_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"
}
如果可能相关,这里是我的活动插件:
首先您需要register_rest_fields在 WP REST 中添加自定义端点APIJSON响应
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,
)
);
}
然后将函数定义为 get custom fields
function get_custom_fields( $object, $field_name, $request ) {
//your code goes here
return $customfieldvalue;
}
在本地站点测试
只需将它添加到您的 CMS 中,然后它就会在您的 WP REST 中可用 API。
将此插件用于此解决方案:
https://wordpress.org/plugins/acf-to-rest-api/ as it allows you to expose the Advanced Custom Fields into their own JSON response via new endpoints listed here https://github.com/airesvsg/acf-to-rest-api#endpoints。然后,您可以 forkJoin
两个可观察值(如果使用 Angular)并利用组合响应来达到您的目的。
我发现 REST API Custom Fields 插件对此很有用。
假设我们要将 'sku' 和 'price' 元添加到 'product' postType
rest_api url 是:
site_addresses.com/wp-json/wp/v2/product
将此代码放在 functions.php
function af_create_custom_meta_in_REST()
{
$post_types = ["product"]; //post types that will include custom fields
foreach ($post_types as $post_type) {
register_rest_field(
$post_type,
'custom_fields',
[
'get_callback' => 'expose_custom_fields',
'schema' => null,
]
);
}
}
function expose_custom_fields($object)
{
$fields = ["_sku", "_price"]; //custom fields name
$post_ID = $object['id'];
$return_value = [];
foreach ($fields as $field) {
$field = strval($field);
$meta = get_post_meta($post_ID, $field);
if (count($meta) > 1)
$return_value[$field] = $meta;
else if ($meta[0])
$return_value[$field] = $meta[0];
else
$return_value[$field] = ""; //if you set this to null result will be 'null'
}
if ($return_value)
return $return_value;
return []; //if you set this to null result will be 'null'
}
add_action('rest_api_init', 'af_create_custom_meta_in_REST');
您可以将 $post_types 或 $fields 更改为您想要的任何内容
结果图片:
image of rest_api reslut
对于使用高级自定义字段的用户:this is now offered out of the box as of v5.11 per-field 个组。步骤:
- 编辑要添加到 REST 的字段组 API
- 在
Settings
下,单击 Show in REST API
要fine-grain控制 REST API 显示的内容和时间,请参阅 ACF 网站上的 Documentation > Guides > WP REST API Integration。这包括如何 include/exclude 字段组中的各个字段的示例。
我想在不使用任何插件的情况下执行此操作,因为这些都是核心 wordpress 功能(自定义字段和 REST API)。以下是自定义字段的文档供参考:
https://codex.wordpress.org/Using_Custom_Fields
这是我的 wordpress 安装截图:
这是 API 对 post 的响应目前的样子:
{
"_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"
}
如果可能相关,这里是我的活动插件:
首先您需要register_rest_fields在 WP REST 中添加自定义端点APIJSON响应
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,
)
);
}
然后将函数定义为 get custom fields
function get_custom_fields( $object, $field_name, $request ) {
//your code goes here
return $customfieldvalue;
}
在本地站点测试
只需将它添加到您的 CMS 中,然后它就会在您的 WP REST 中可用 API。
将此插件用于此解决方案:
https://wordpress.org/plugins/acf-to-rest-api/ as it allows you to expose the Advanced Custom Fields into their own JSON response via new endpoints listed here https://github.com/airesvsg/acf-to-rest-api#endpoints。然后,您可以 forkJoin
两个可观察值(如果使用 Angular)并利用组合响应来达到您的目的。
我发现 REST API Custom Fields 插件对此很有用。
假设我们要将 'sku' 和 'price' 元添加到 'product' postType rest_api url 是:
site_addresses.com/wp-json/wp/v2/product
将此代码放在 functions.php
function af_create_custom_meta_in_REST()
{
$post_types = ["product"]; //post types that will include custom fields
foreach ($post_types as $post_type) {
register_rest_field(
$post_type,
'custom_fields',
[
'get_callback' => 'expose_custom_fields',
'schema' => null,
]
);
}
}
function expose_custom_fields($object)
{
$fields = ["_sku", "_price"]; //custom fields name
$post_ID = $object['id'];
$return_value = [];
foreach ($fields as $field) {
$field = strval($field);
$meta = get_post_meta($post_ID, $field);
if (count($meta) > 1)
$return_value[$field] = $meta;
else if ($meta[0])
$return_value[$field] = $meta[0];
else
$return_value[$field] = ""; //if you set this to null result will be 'null'
}
if ($return_value)
return $return_value;
return []; //if you set this to null result will be 'null'
}
add_action('rest_api_init', 'af_create_custom_meta_in_REST');
您可以将 $post_types 或 $fields 更改为您想要的任何内容
结果图片: image of rest_api reslut
对于使用高级自定义字段的用户:this is now offered out of the box as of v5.11 per-field 个组。步骤:
- 编辑要添加到 REST 的字段组 API
- 在
Settings
下,单击Show in REST API
要fine-grain控制 REST API 显示的内容和时间,请参阅 ACF 网站上的 Documentation > Guides > WP REST API Integration。这包括如何 include/exclude 字段组中的各个字段的示例。