I’m trying to update a CPT post’s metadata using the REST API. I can update a numeric metadata element using a POST such as this:

example.com/wp-json/wp/v2/posts/73/meta/229?value=2

This method is based on a workaround found here: WP REST API “rest_no_route” when trying to update meta

I can also update the meta data using a post here:
example.com/wp-json/wp/v2/posts/73/meta/229

with this JSON in the body:

{"value":"2"}

The field I NEED to update contains a serialized array of data, though. When I try to update field using the second method (JSON body), I get this error:

{"value": [
{
  "name": "Related Documents",
  "default": true,
  "documents": [
    {
      "label": "RFA - Revit File",
      "file_location": "http://example.com/wp-content/uploads/2017/03/file.pdf"
    }
  ]
}
]
}

I’ve also tried sending my data as a serialized array (which is how it gets stored in MySql).

When I send:

{"value": "{a:0}"}

I get:

{
"code": "rest_post_invalid_action",
"message": "Invalid existing meta data for action.",
"data": {
"status": 400
}
}

Notes:
This metadata is a field that’s part of a WooCommerce Plugin: https://woocommerce.com/products/product-documents/

I have this plugin installed to expose my custom post types to the API (and I’m able to RETRIEVE the data): wordpress.org/plugins/wp-rest-api-controller/

I also have this plugin installed to allow access to the meta endpoints: wordpress.org/plugins/rest-api-meta-endpoints/

My end goal here is to stuff new data into the MySql database for this custom field. I’m learning that the WP REST API is still very undocumented and rather weak when it comes to meta data, especially (1) for custom post types, and (2) for non-string/numeric data types.

I’m considering writing a basic PHP page that will take a product ID, find the correct meta record using a WordPress function, and updating the data by turning posted JSON into a serialized array, but I’m not familiar enough with PHP to do this, and I’d also have to take some security things into account. Any examples or ideas on how to get this started, though, or if there’s a better way to get my metadata updated?

Read more here: Update CPT meta data using REST API


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: