I have been working with the WP REST API and all GET (read) commands pull the data I want from both built in and custom post types (this plugin was a godsend btw to help). I can also POST to create posts with top level information for both built-in and custom post types. All my authentication is working fine. But when I try to update post meta, I have been met by the following response:
{
"code": "rest_no_route",
"message": "No route was found matching the URL and request method",
"data": {
"status": 404
}
}
I found reference in this article for the potential need for some extra code to make meta work, and I then found this plugin which DID solve the meta posting problem, but only for the built in posts type (not working with my custom post types, which still return exactly the same error).
All of the above is confusing and compounded I think by the changing capabilities and state of the WP REST API itself.
Can anyone point me to some clear documentation for performing a meta value update on a custom post type?
UPDATE:
ok I just discovered that the rest-api-meta-endpoints plugin mentioned above WILL allow writing to CPTs as well, but via /wp-json/wp/v2/posts/id/meta instead of /wp-json/wp/v2/cptname/id/meta…is this the expected behavior?
That said, for any posts I can only write new data, can’t figure out how to update existing meta yet, any ideas?
Here is what I have learned about the WP REST API: It is a mess of undocumented and unfinished code with great promise but frustratingly little clarity.
That said, I have a workaround that I will post here, hoping it is useful to others in a similar pickle to me:
I just found that I can update a meta field IF I have its id and IF I use posts
in my path (even for cpts). So, for example (assuming my post id is 1622 and my meta id is 11395), this query will work:
POST https://example.com/wp-json/wp/v2/posts/1622/meta/11395?value=mykeyvalue
but these will NOT (in various ways):
POST https://example.com/wp-json/wp/v2/posts/1622/meta?key=mykeyname&value=mykeyvalue (will ADD new but not edit existing)
POST https://example.com/wp-json/wp/v2/posts/1622/?key=mykeyname&value=mykeyvalue (404)
POST https://example.com/wp-json/wp/v2/my-cpt/1622/meta?key=mykeyname&value=mykeyvalue (404)
POST https://example.com/wp-json/wp/v2/my-cpt/1622/meta/11395?value=mykeyvalue (404)
I also figured out that I can GET all the meta by performing a query like this:
GET https://example.com/wp-json/wp/v2/posts/1622/meta/
So I guess putting it all together, I could make this work in the current form by:
-
GET https://example.com/wp-json/wp/v2/posts/1622/meta/
-
filtering the above result to get the meta id that I want to change
-
POST https://example.com/wp-json/wp/v2/posts/1622/meta/11395?value=mykeyvalue
If anyone has anything to add or any thoughts about any possible other directions, I am all ears. Otherwise, I guess this is my “solution”.
And please note the prerequisites for even getting this to work:
- This plugin configured to expose your post types and meta
- This plugin to enable meta endpoints.
- What I didn’t include above was any mention of authentication, as it is outside the scope of this discussion, but depending on your settings, you may need to authenticate before using the API, as is the case in my setup (using Oauth and tokens which would normally be added to these URLs).