使用WP REST API编辑元字段

发布于 2025-02-09 15:02:34 字数 1725 浏览 2 评论 0原文

这是我的第一篇文章,所以如果我做错了,我深表歉意。我目前正在自己​​的网站上工作,我正在尝试更新,创建和删除属性类型条目。我几乎可以毫无问题地执行所有操作,但是我无法编辑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"
            ]
    }
}'

image在密钥

邮递员返回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"
            ]
    }
}'

Image of the key

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花之痕靓丽 2025-02-16 15:02:34

高级自定义字段插件给了我生命,只需添加我要修改或使用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.

1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文