通过API获取方法时的响应:
"id":"12680",
"title": "TESTING Property",
"content": "Description of the property 123",
"status": "publish",
"property_meta": {
"fave_currency_info": [
""
],
"fave_property_price": [
"320000"
],
"fave_property_price_postfix": [
"Sq Ft"
],
"fave_property_sec_price": [
"1500"
]
}
**Auth **:基本身份验证 **正文**:原始数据=>
{
"title": "Test New Moulee Property",
"content": "Description of the property",
"status": "publish",
"property_meta":
{
"fave_fave_property_price":["3511111"],
"fave_property_price_prefix":["586776"],
"fave_property_price_postfix" : ["7888877"],
"fave_property_size" : ["65"],
"fave_property_size_prefix" : ["4666"]
}
}
方式:邮寄
通过API发布方法时的响应:
"id":"12677",
"title": "Test New Moulee Property",
"content": "Description of the property",
"status": "publish",
"property_meta": {
"fave_property_price": [
""
],
"fave_property_price_postfix": [
""
],
"fave_property_sec_price": [
""
]
}
实际问题是通过postman api在wordpress(houzez主题)中添加记录时,它将成功创建记录,但在记录值中property_meta字段中出现空值。
我们预计 property_meta 字段值也需要添加到其中。
这是我通过 API 尝试过的邮递员集合。
/*------------------------------------------------
* 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;
}
}
在 POST 请求中,您使用元字段名称,例如 fave_fave_property_price、fave_property_price_prefix 等。但是,在 GET 响应中,名称略有不同 例如,fave_property_price、fave_property_price_postfix
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,
));
}
function houzez_get_rest_api_property_meta( $object ) {
$post_id = $object['id'];
$property_meta = get_post_meta( $post_id );
$property_meta = apply_filters( 'houzez_property_rest_api_meta', $property_meta );
return $property_meta;
}
function houzez_update_rest_api_property_meta($value, $object, $field_name) {
if (!is_array($value) || !$object instanceof WP_Post) {
return;
}
foreach ($value as $meta_key => $meta_value) {
update_post_meta($object->ID, $meta_key, $meta_value);
}
}
}