使用WP REST API编辑元字段
这是我的第一篇文章,所以如果我做错了,我深表歉意。我目前正在自己的网站上工作,我正在尝试更新,创建和删除属性类型条目。我几乎可以毫无问题地执行所有操作,但是我无法编辑property_meta字段。
这是我认为我应该编辑字段的命令,因为其他不是元数据的字段确实有效:
curl --location --request PUT 'https://myweb.com/wp-json/wp/v2/properties/18493' \
--header 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvY2hvbGxvY2l0eS5lcyIsImlhdCI6MTY1NTkwNTg5MSwibmJmIjoxNjU1OTA1ODkxLCJleHAiOjE2NTY1MTA2OTEsImRhdGEiOnsidXNlciI6eyJpZCI6IjIifX19.J6-R8LQslxWINaSR6alTAQEkxQrDCykx0ZkfSSsjOSU' \
--header 'Content-Type: application/json' \
--data-raw '{
"property_meta": {
"fave_property_price": [
"33"
]
}
}'
邮递员返回200的电话中,我无法获得更改的价值。感谢您的帮助,
我设法发现在其余API中显示此元数据的PHP代码如下:
/*------------------------------------------------
* Properties Meta Fields for rest API
*----------------------------------------------- */
if( !function_exists('houzez_property_rest_api_field')) {
add_action( 'rest_api_init', 'houzez_property_rest_api_field' );
function houzez_property_rest_api_field() {
register_rest_field( 'property', 'property_meta', array(
'get_callback' => 'houzez_get_rest_api_property_meta'
) );
}
function houzez_get_rest_api_property_meta( $object ) {
$post_id = $object['id'];
$property_meta = get_post_meta( $post_id );
// add filter
$property_meta = apply_filters( 'houzez_property_rest_api_meta', $property_meta );
// return meta
return $property_meta;
}
}
但是我仍然无法通过API REST编辑它们
this is my first post so I apologize if I do wrong. I am currently working on a website of my own in which I am trying to update, create and delete property type entries. I can perform almost everything without problem but I am unable to edit the property_meta field.
This is the command that I think I should edit the field, as other fields that are not metadata do work:
curl --location --request PUT 'https://myweb.com/wp-json/wp/v2/properties/18493' \
--header 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvY2hvbGxvY2l0eS5lcyIsImlhdCI6MTY1NTkwNTg5MSwibmJmIjoxNjU1OTA1ODkxLCJleHAiOjE2NTY1MTA2OTEsImRhdGEiOnsidXNlciI6eyJpZCI6IjIifX19.J6-R8LQslxWINaSR6alTAQEkxQrDCykx0ZkfSSsjOSU' \
--header 'Content-Type: application/json' \
--data-raw '{
"property_meta": {
"fave_property_price": [
"33"
]
}
}'
The call with postman returns 200, but I can't get the value to change. Thanks for your help
I have managed to find out that the php code that displays this metadata in the REST API is the following:
/*------------------------------------------------
* Properties Meta Fields for rest API
*----------------------------------------------- */
if( !function_exists('houzez_property_rest_api_field')) {
add_action( 'rest_api_init', 'houzez_property_rest_api_field' );
function houzez_property_rest_api_field() {
register_rest_field( 'property', 'property_meta', array(
'get_callback' => 'houzez_get_rest_api_property_meta'
) );
}
function houzez_get_rest_api_property_meta( $object ) {
$post_id = $object['id'];
$property_meta = get_post_meta( $post_id );
// add filter
$property_meta = apply_filters( 'houzez_property_rest_api_meta', $property_meta );
// return meta
return $property_meta;
}
}
But I still can't edit them via API REST
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
高级自定义字段插件给了我生命,只需添加我要修改或使用API工作创建的字段的过滤器即可。
The Advanced Custom Fields plugin has given me life, simply adding a filter of the field I want to modify or create with the API works.