从指南看来,开箱即用不支持读取和写入自定义字段。
register_meta 用于将现有自定义元值列入白名单
通过 REST API 访问。通过设置元字段的 show_in_rest
参数为 true,该字段的值将在 .meta 键上公开
在端点响应中,和 WordPress 将处理设置
用于读取和写入该元键的回调。
所以你必须添加一些php来注册一个元密钥,然后它是可读可写的。
在同一页面上有这个示例代码:
<?php
// The object type. For custom post types, this is 'post';
// for custom comment types, this is 'comment'. For user meta,
// this is 'user'.
$object_type = 'post';
$meta_args = array( // Validate and sanitize the meta value.
// Note: currently (4.7) one of 'string', 'boolean', 'integer',
// 'number' must be used as 'type'. The default is 'string'.
'type' => 'string',
// Shown in the schema for the meta key.
'description' => 'A meta key associated with a string meta value.',
// Return a single value of the type.
'single' => true,
// Show in the WP REST API response. Default: false.
'show_in_rest' => true,
);
register_meta( $object_type, 'my_meta_key', $meta_args );
您可以将其放入您的theme_functions 页面。请参阅外观 > 主题编辑器。
然后在您的代码中定义meta 属性并使用名称值对发送它;键的名称和它的值。
var results = await fetch(url, {
method: "POST",
headers:{
'Content-Type': 'application/json',
'accept': 'application/json',
'Authorization': 'Bearer '+ token
},
body: JSON.stringify({
title: pageTitle,
content: markupOutput,
status: 'publish',
meta: {
my_meta_key: "test"
}
})
})
示例代码:
https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/#read-and-write-a-post-meta-field-in-post-responses
文档:
https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/#using-register_rest_field-vs-register_meta