How to pass the values into META field "property_meta" via api in Wordpress for (Houzez theme)

43 Views Asked by At

Response while get method via 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 **: basic auth **Body **: raw data =>

{
  "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"]
  }
  
}

Method : Post

Response while post method via 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": [
            ""
        ]
}

Actual Issues is while adding the record in wordpress(houzez theme) via postman api, It will create a record successfully, But in the record values there is an empty values occur in property_meta field.

we are expecting the property_meta field values also need to add in it.

This is the postman collection which I've tried via API.

https://special-task-force.postman.co/workspace/Zoho-CRM-to-Wordpress~3cd6acb9-4e6a-457d-aba1-fe1aa7a10619/collection/25739789-0b8c76f0-d441-4f01-baca-58fd062a4407?action=share&creator=31749745

/*------------------------------------------------
 * 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;
    }
}
1

There are 1 best solutions below

0
Jenny On

In your POST request, you're using meta field names like fave_fave_property_price, fave_property_price_prefix, etc. However, in your GET response, the names are slightly different e.g., 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);
        }
    }
}