You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress Houzez主题API写入property_meta字段值失败问题

解决Houzez主题REST API提交房源时property_meta字段无法写入的问题

问题概述

通过Postman以Basic Auth认证调用Houzez主题的REST API,POST请求能成功创建房源记录,但请求体中property_meta的自定义值未被写入数据库,返回结果里该字段对应值为空。

原因分析

  1. REST字段配置缺失写入逻辑:当前注册property_meta字段的代码仅定义了get_callback(读取逻辑),没有设置update_callback来处理POST/PUT请求时的元数据写入操作。
  2. 请求体字段名错误:请求体中存在字段名重复问题——fave_fave_property_price多了一个前缀fave_,正确字段名应为fave_property_price。

解决方案

1. 修正请求体字段名

将POST请求体里的"fave_fave_property_price":[..."改为"fave_property_price":[...",修正后的请求体示例:

{
  "title": "Test New Moulee Property",
  "content": "Description of the property",
  "status": "publish",
  "property_meta": 
  {
        "fave_property_price":["3511111"],
        "fave_property_price_prefix":["586776"],
        "fave_property_price_postfix" : ["7888877"],
        "fave_property_size" : ["65"],
        "fave_property_size_prefix" : ["4666"]
  }
}

2. 更新REST API字段注册代码

在原代码中添加update_callback,实现元数据的写入逻辑,同时处理数组格式的元数据存储(WordPress默认将数组元数据序列化存储):

/*------------------------------------------------
 * 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',
            'update_callback' => 'houzez_update_rest_api_property_meta', // 添加写入回调
            'schema' => null, // 可根据需求定义字段schema,这里暂时设为null
        ) );
    }

    function houzez_get_rest_api_property_meta( $object ) {
        $post_id = $object['id'];
        $property_meta = get_post_meta( $post_id );

        // 过滤元数据,移除WordPress自动添加的私有元数据(可选)
        $filtered_meta = array();
        foreach($property_meta as $key => $value) {
            if(strpos($key, '_') !== 0) {
                $filtered_meta[$key] = $value;
            }
        }

        $filtered_meta = apply_filters( 'houzez_property_rest_api_meta', $filtered_meta );
        return $filtered_meta;
    }

    // 新增写入回调函数
    function houzez_update_rest_api_property_meta( $value, $post, $field_name ) {
        if(empty($value) || !is_array($value)) {
            return false;
        }

        $post_id = $post->ID;
        foreach($value as $meta_key => $meta_value) {
            // 匹配get_post_meta返回的数组结构,存储对应值
            if(is_array($meta_value)) {
                update_post_meta($post_id, $meta_key, $meta_value[0]);
            } else {
                update_post_meta($post_id, $meta_key, $meta_value);
            }
        }
        return true;
    }
}

3. 验证修改效果

  • 重新发送修正后的POST请求
  • 检查返回结果中的property_meta字段,确认自定义值已正确返回
  • 登录WordPress后台,查看对应房源的自定义字段,确认元数据已存储

内容的提问来源于stack exchange,提问作者Kishore Christi232

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 19:03:38