我正在尝试在 Wordpress RestAPI 中注册自定义帖子类型。我已经完成了大部分工作(帖子本身和字段),但我仍然坚持让中继器字段注册。这是我到目前为止所拥有的。
这段代码位于我的主题文件夹中的 functions.php 文件中。我取出了一堆字段来缩短代码,因为它们都是字符串,并且就像给定字段“reservation-status”一样工作。
add_action( 'init', 'booking_register_meta' );
function booking_register_meta(){
//This bit registers my custom post type
register_post_type(
'reservations',
array(
'labels' => array(
'name' => _( 'Reservations' ),
'singular_name' => _( 'Reservation' ),
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'supports' => array(
'title',
'custom-fields',
'revisions',
)
)
);
//this bit registers my regular field
register_meta(
'post',
'reservation-status',
array(
'single' => true,
'type' => 'string',
'default' => '',
'show_in_rest' => true,
'object_subtype' => 'reservations'
)
);
//this bit is suppose to register my repeater field
register_post_meta(
'post',
'occupants',
array(
'single' => false,
'type' => 'array',
'show_in_rest' => array(
'schema' => array(
'items' => array(
'type' => 'string',
'properties' => array(
'occupant-first-name' => array(
'type' => 'string',
),
'occupant-last-name' => array(
'type' => 'string',
),
),
),
),
),
)
);
}
我正在使用这里的文档:Wordpress:修改响应
这是我使用 postman 时的 JSON 输出。(没有值的字段是正确的)。注意:元中缺少占用者。这是一个 GET 请求,该帖子的 resident Repeater 字段有信息输入。
{
"id": 88737,
"date": "2023-10-11T03:47:52",
"date_gmt": "2023-10-11T07:47:52",
"guid": {
"rendered": "https://example.com/reservations/myemailgmail-com/"
},
"modified": "2023-10-15T22:43:12",
"modified_gmt": "2023-10-16T02:43:12",
"slug": "myemailgmail-com",
"status": "publish",
"type": "reservations",
"link": "https://example.com/reservations/myemailgmail-com/",
"title": {
"rendered": "[email protected]"
},
"content": {
"rendered": "",
"protected": false
},
"template": "",
"meta": {
"_jf_save_progress": "",
"reservation-status": "Pending Reservation",
"reservation-number": "",
"confirmed-reservation-id": "",
"check-in": "2023-10-11",
"check-out": "2023-10-18",
"resort": "Bay Lake Tower",
"room": "Deluxe Studio",
"view": "Standard View",
"room-request": "",
"renter-first-name": "John",
"renter-last-name": "Doe",
"renter-full-name": "John Doe",
"email": "[email protected]",
"phone": "11231231234",
"address": "Platform 9 3/4",
"state": "",
"points": "",
"price-per-point": "",
"deposit": "",
"deposit-status": "",
"total": "",
"total-with-fee": "",
"balance-status": "",
"balance": "",
"balance-with-fee": "",
"balance-due-date": "",
"date-balance-paid": "",
"paypal-fee": "",
"payment-method": "",
"travel-credit-number": "",
"promo-code": "",
"contract-status": "",
"7-month-window": "",
"11-month-window": "",
"comments": ""
},
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wp/v2/reservations/88737"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wp/v2/reservations"
}
],
"about": [
{
"href": "https://example.com/wp-json/wp/v2/types/reservations"
}
],
"version-history": [
{
"count": 1,
"href": "https://example.com/wp-json/wp/v2/reservations/88737/revisions"
}
],
"predecessor-version": [
{
"id": 88739,
"href": "https://example.com/wp-json/wp/v2/reservations/88737/revisions/88739"
}
],
"wp:attachment": [
{
"href": "https://example.com/wp-json/wp/v2/media?parent=88737"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
}
]
如果您查看
register_post_meta()
函数,第一个参数应该是元字段将注册的帖子类型。目前,您正在将 occupants
字段添加到 post
帖子类型。
从您的代码看来,您的自定义帖子类型是
reservations
,因此您应该为 occupants
帖子类型注册 reservations
字段。
类似:
register_post_meta(
'reservations',
'occupants',
array(
'single' => false,
'type' => 'array',
'show_in_rest' => array(
'schema' => array(
'items' => array(
'type' => 'string',
'properties' => array(
'occupant-first-name' => array(
'type' => 'string',
),
'occupant-last-name' => array(
'type' => 'string',
),
),
),
),
),
)
);
此外,以单数形式命名自定义帖子类型也是一个好习惯。