1

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?

Edited changed my getcallback function with reference from https://developer.wordpress.org/reference/functions/register_rest_field/

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( $object['id'], 'fave_property_images', false) ;
}

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!

2nd Edit I am currently using postman to post just for testing purposes. This is a sample json that I am sending

{
  "status": "publish",
  "type": "property",
  "title": {
    "raw": "test"
  },
  "content": {
    "raw": "<p>Singapore Management University (SMU) is the third autonomous university in Singapore. The university provides broad-based business programmes modelled after the Wharton School of the University of Pennsylvania. SMU was established on 29 July 2000 and is located in the Downtown area of Singapore. It has a city campus with a total enrolment of about 10,000 undergraduate and postgraduate students and comprises six schools offering undergraduate, graduate and PhD programmes in business administration, business analytics, financial services, accountancy, economics, information systems management, law and the social sciences. The university has over 30 research institutes and centres of excellence, and customised corporate training and lifelong learning for individuals are available through the university&#8217;s professional and executive development programmes. SMU is accredited by the AACSB International, EQUIS, and AMBA.</p>\n",
    "protected": False
  },
  "excerpt": {
    "raw": "<p>excerpt</p>\n",
    "protected": False
  },
  "author": 1,
  "featured_media": 18297,
  "parent": 0,
  "menu_order": 0,
  "template": "",
  "property_type": [
    1072,
    1073,
    1074,
    1071
  ],
  "property_status": [],
  "property_feature": [
    1079,
    1080,
    1081,
    1082
  ],
  "property_label": [],
  "property_country": [
    1065
  ],
  "property_city": [
    1357
  ],

  "fave_qs-ranking": "50-100",
  "fave_percentage-employed-after-graduation": "90%",
  "fave_school-fees": "$23000",
  "fave_local-to-international-students": "1:1",
  "fave_ielts-toefl": "10:10",
  "fave_times_ranking": "3",
  "fave_minimum-gpa": "23",
  "fave_property_images": ["18297","18337","17855"],
  "fave_property_price": "$200",
  "fave_property_map_address": "81 Victoria St, Singapore 188065",
  "fave_property_address": "81 Victoria St",
  "fave_property_zip": "188065"

}

3rd edit I have managed to display the array on fave_property_images field, however the field in property_meta remains a serialized array And this is the resultant sample json from /wp-json/wp/v2/schools

{
  "id": 18397,
  "date": "2021-07-24T16:50:22",
  "date_gmt": "2021-07-24T08:50:22",
  "guid": {
    "rendered": "https://www.abroad.me/index.php/school/test/"
  },
  "modified": "2021-07-24T16:50:22",
  "modified_gmt": "2021-07-24T08:50:22",
  "slug": "test",
  "status": "publish",
  "type": "property",
  "link": "https://www.abroad.me/index.php/school/test/",
  "title": {
    "rendered": "test"
  },
  "content": {
    "rendered": "<p>Singapore Management University (SMU) is the third autonomous university in Singapore. The university provides broad-based business programmes modelled after the Wharton School of the University of Pennsylvania. SMU was established on 29 July 2000 and is located in the Downtown area of Singapore. It has a city campus with a total enrolment of about 10,000 undergraduate and postgraduate students and comprises six schools offering undergraduate, graduate and PhD programmes in business administration, business analytics, financial services, accountancy, economics, information systems management, law and the social sciences. The university has over 30 research institutes and centres of excellence, and customised corporate training and lifelong learning for individuals are available through the university&#8217;s professional and executive development programmes. SMU is accredited by the AACSB International, EQUIS, and AMBA.</p>\n",
    "protected": false
  },
  "excerpt": {
    "rendered": "<p>excerpt</p>\n",
    "protected": false
  },
  "author": 1,
  "featured_media": 18297,
  "parent": 0,
  "menu_order": 0,
  "template": "",
  "property_type": [
    1072,
    1073,
    1074,
    1071
  ],
  "property_status": [],
  "property_feature": [
    1079,
    1080,
    1081,
    1082
  ],
  "property_label": [],
  "property_country": [
    1065
  ],
  "property_city": [
    1357
  ],
  "property_meta": {
            "_thumbnail_id": [
                "18397"
            ],
            "_wp_page_template": [
                ""
            ],
            "fave_property_images": [
                "a:3:{i:0;s:5:\"18297\";i:1;s:5:\"18337\";i:2;s:5:\"17855\";}"
            ],
            "fave_minimum-gpa": [
                "23"
            ],
            "fave_property_price": [
                "$200"
            ],
            "fave_property_map_address": [
                "81 Victoria St, Singapore 188065"
            ],
            "fave_property_address": [
                "81 Victoria St"
            ],
            "fave_property_zip": [
                "188065"
            ],
            "fave_qs-ranking": [
                "50-100"
            ],
            "fave_percentage-employed-after-graduation": [
                "90%"
            ],
            "fave_school-fees": [
                "$23000"
            ],
            "fave_local-to-international-students": [
                "1:1"
            ],
            "fave_ielts-toefl": [
                "10:10"
            ]
        },
        "fave_property_images": [
            "18297",
            "18337",
            "17855"
        ],
        "fave_minimum-gpa": "23",
        "fave_times-ranking": "",
        "fave_property_year": "",
        "fave_property_price": "$200",
        "fave_property_location": "",
        "fave_houzez_geolocation_lat": "",
        "houzez_geolocation_long": "",
        "fave_property_map_address": "81 Victoria St, Singapore 188065",
        "fave_property_address": "81 Victoria St",
        "fave_property_zip": "188065",
        "fave_qs-ranking": "50-100",
        "fave_percentage-employed-after-graduation": "90%",
        "fave_school-fees": "$23000",
        "fave_local-to-international-students": "1:1",
        "fave_ielts-toefl": "10:10",
  "_links": {
    "self": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/schools/18397"
      }
    ],
    "collection": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/schools"
      }
    ],
    "about": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/types/property"
      }
    ],
    "author": [
      {
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/users/1"
      }
    ],
    "version-history": [
      {
        "count": 0,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/schools/18397/revisions"
      }
    ],
    "wp:featuredmedia": [
      {
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/media/18297"
      }
    ],
    "wp:attachment": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/media?parent=18397"
      }
    ],
    "wp:term": [
      {
        "taxonomy": "property_type",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_type?post=18397"
      },
      {
        "taxonomy": "property_status",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_status?post=18397"
      },
      {
        "taxonomy": "property_feature",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_feature?post=18397"
      },
      {
        "taxonomy": "property_label",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_label?post=18397"
      },
      {
        "taxonomy": "property_country",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_country?post=18397"
      },
      {
        "taxonomy": "property_city",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_city?post=18397"
      }
    ],
    "curies": [
      {
        "name": "wp",
        "href": "https://api.w.org/{rel}",
        "templated": true
      }
    ]
  }
}

However if I add images by editing the post on wordpress, the json would be as follows and the field fave_property_images under property_meta would just be an array.

{
  "id": 18367,
  "date": "2021-07-23T11:13:46",
  "date_gmt": "2021-07-23T03:13:46",
  "guid": {
    "rendered": "https://www.abroad.me/index.php/school/test-8/"
  },
  "modified": "2021-07-23T14:52:49",
  "modified_gmt": "2021-07-23T06:52:49",
  "slug": "test-8",
  "status": "publish",
  "type": "property",
  "link": "https://www.abroad.me/index.php/school/test-8/",
  "title": {
    "rendered": "test"
  },
  "content": {
    "rendered": "<p>Singapore Management University (SMU) is the third autonomous university in Singapore. The university provides broad-based business programmes modelled after the Wharton School of the University of Pennsylvania. SMU was established on 29 July 2000 and is located in the Downtown area of Singapore. It has a city campus with a total enrolment of about 10,000 undergraduate and postgraduate students and comprises six schools offering undergraduate, graduate and PhD programmes in business administration, business analytics, financial services, accountancy, economics, information systems management, law and the social sciences. The university has over 30 research institutes and centres of excellence, and customised corporate training and lifelong learning for individuals are available through the university’s professional and executive development programmes. SMU is accredited by the AACSB International, EQUIS, and AMBA.</p>\n",
    "protected": false
  },
  "excerpt": {
    "rendered": "<p>excerpt</p>\n",
    "protected": false
  },
  "author": 1,
  "featured_media": 18297,
  "parent": 0,
  "menu_order": 0,
  "template": "",
  "property_type": [
    1072,
    1073,
    1074,
    1071
  ],
  "property_status": [],
  "property_feature": [
    1079,
    1080,
    1081,
    1082
  ],
  "property_label": [],
  "property_country": [
    1065
  ],
  "property_city": [
    1357
  ],
  "property_meta": {
    "_thumbnail_id": [
      "18297"
    ],
    "_wp_page_template": [
      ""
    ],
    "fave_percentage-employed-after-graduation": [
      "90%"
    ],
    "fave_school-fees": [
      "$23000"
    ],
    "fave_local-to-international-students": [
      "1:1"
    ],
    "fave_ielts-toefl": [
      "10:10"
    ],
    "fave_minimum-gpa": [
      "23"
    ],
    "fave_property_price": [
      "$200"
    ],
    "fave_property_map_address": [
      "81 Victoria St, Singapore 188065"
    ],
    "fave_property_address": [
      "81 Victoria St"
    ],
    "fave_property_zip": [
      "188065"
    ],
    "houzez_total_property_views": [
      "39"
    ],
    "houzez_views_by_date": [
      "a:2:{s:10:\"07-23-2021\";i:38;s:10:\"07-24-2021\";i:1;}"
    ],
    "houzez_recently_viewed": [
      "2021-07-24 12:15:46"
    ],
    "_edit_lock": [
      "1627030465:1"
    ],
    "_edit_last": [
      "1"
    ],
    "fave_property_location": [
      "1.2977432527616,103.84889449988,18"
    ],
    "houzez_geolocation_lat": [
      "1.2977432527616"
    ],
    "houzez_geolocation_long": [
      "103.84889449988"
    ],
    "fave_property_map_street_view": [
      "hide"
    ],
    "fave_agents": [
      "-1"
    ],
    "fave_property_agency": [
      "-1"
    ],
    "fave_single_top_area": [
      "global"
    ],
    "fave_single_content_area": [
      "global"
    ],
    "rs_page_bg_color": [
      ""
    ],
    "houzez_manual_expire": [
      "0"
    ],
    "_houzez_expiration_date_status": [
      "saved"
    ],
    "fave_property_map": [
      "1"
    ],
    "fave_property_images": [
      "18339",
      "18337",
      "18321",
      "17794",
      "17793",
      "17792",
      "17791",
      "17790",
      "17789",
      "17787"
    ]
  },
  "fave_qs-ranking": false,
  "fave_percentage-employed-after-graduation": false,
  "fave_school-fees": false,
  "fave_local-to-international-students": false,
  "fave_ielts-toefl": false,
  "fave_times_ranking": "",
  "fave_property_images": false,
  "fave_minimum-gpa": false,
  "fave_times-ranking": false,
  "fave_property_year": false,
  "fave_property_price": false,
  "fave_property_location": false,
  "fave_houzez_geolocation_lat": false,
  "houzez_geolocation_long": false,
  "fave_property_map_address": false,
  "fave_property_address": false,
  "fave_property_zip": false,
  "_links": {
    "self": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/schools/18367"
      }
    ],
    "collection": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/schools"
      }
    ],
    "about": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/types/property"
      }
    ],
    "author": [
      {
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/users/1"
      }
    ],
    "version-history": [
      {
        "count": 0,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/schools/18367/revisions"
      }
    ],
    "wp:featuredmedia": [
      {
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/media/18297"
      }
    ],
    "wp:attachment": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/media?parent=18367"
      }
    ],
    "wp:term": [
      {
        "taxonomy": "property_type",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_type?post=18367"
      },
      {
        "taxonomy": "property_status",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_status?post=18367"
      },
      {
        "taxonomy": "property_feature",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_feature?post=18367"
      },
      {
        "taxonomy": "property_label",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_label?post=18367"
      },
      {
        "taxonomy": "property_country",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_country?post=18367"
      },
      {
        "taxonomy": "property_city",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_city?post=18367"
      }
    ],
    "curies": [
      {
        "name": "wp",
        "href": "https://api.w.org/{rel}",
        "templated": true
      }
    ]
  }
}
9
  • how do you mean it becomes serialised? Where are you getting the serialised line of code from?
    – Paul G.
    Commented Jul 23, 2021 at 11:46
  • Is that your actual code? Because in your get_post_meta_for_api() function, both $post and $field_name are undefined. Also, the first parameter for the function is not an object and instead, it's an array. See the example here.
    – Sally CJ
    Commented Jul 23, 2021 at 12:02
  • I am getting the serialised line of code from my API end point /wp-json/wp/v2/schools
    – user209330
    Commented Jul 24, 2021 at 3:28
  • How did you post to the REST API - via JS, PHP, Postman, or? Can you edit your question and include the details including sample post data you posted? And how did you retrieve data from the REST API? Also, do your posts have multiple fave_property_images meta entries? If not, then you should call get_post_meta() with true being the third parameter.
    – Sally CJ
    Commented Jul 24, 2021 at 6:00
  • I have updated the additional information above. I dont really know if my posts has multiple fave_property_images, my sample post json is above. But i can assure you i have tried both true and false parameters for get_most_meta() and they both didnt work.
    – user209330
    Commented Jul 24, 2021 at 9:17

1 Answer 1

0

So I found my own answer, fave_property_images is not supposed to be an array, its just multiple strings. Therefore the following codes in function.php fixes the problem.

function add_fave_property_images() {
    register_rest_field('property',
        'fave_property_images',
        array(
            'get_callback' => 'get_property_images',
            'update_callback' => 'update_property_images',
        )
    );
}

function get_property_images( $object ) {
    //return the post meta
    return get_post_meta( $object['id'], 'fave_property_images', false);
}
function update_property_images($value, $post, $field_name) {
  // Update the field
    foreach($value as $ID){
        add_post_meta($post->ID, $field_name, $ID);
    }
    return;
  
}

add_action('rest_api_init', 'add_fave_property_images');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.