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).