How to post an unserialized array via wordpress rest API as meta data

Question

I am new to wordpress and am currently using WordPress Rest API to help automatically create custom posts using existing data. 1 of the fields needed is an array of image IDs so that images can be displayed. If a post is manually created on wordpress with the images, I know that the meta data would need to be in this format

"fave_property_images":["18339","18337","18321","17794","17793","17792","17791","17790","17789","17787"]

However, after posting through WordPress Rest API, the meta data automatically becomes serialized as such and no image is displayed.

"fave_property_images":["a:3:{i:0;s:5:"18297";i:1;s:5:"18337";i:2;s:5:"17855";}"]

Is there a way to prevent the auto serialization of my data?

functions.php

function add_fave_property_images() {
    register_rest_field('property',
        'fave_property_images',
        array(
            'get_callback' => 'get_post_meta_for_api',
            'update_callback' => 'rest_update_property_meta',
            'schema' => array(
                                'type'  => 'array',
                                'items' => array(
                                    'type'   => 'string',
                                    ),
                            ),
        )
    );
}

function get_post_meta_for_api( $object ) {
    //return the post meta
    return get_post_meta( $post->ID, $field_name);
}

function rest_update_property_meta($value, $post, $field_name) {

  // Update the field
  return update_post_meta($post->ID, $field_name, $value);
}

I have also tried maybe_unserialize() to no avail. I would appreciate any help thank you!

0
user209330 2 months 2021-07-23T05:00:45-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse