3

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?

  • Does anyone know if you can get usermeta in this way? I'd love for the user to be able to update their own data... something like wp-json/wp/v2/users/me/meta?key=name&value=Newname? – Armstrongest Dec 5 '17 at 19:01
  • Great solution but one of the plugins is not maintained anymore. Does anyone know an alternative to wordpress.org/plugins/rest-api-meta-endpoints please? Or is there an already better solution now? I have spent many hours researching how to update custom fields in wordpress and this is the only solution I have found. – Paul C Sep 5 '19 at 15:41
  • tbh, I wrote this so long ago that I would doubt that the same limitations even exist in the core REST API code. There are probably cleaner solutions today without the need to goo into such contortions. – Stephen Sep 5 '19 at 19:03
6

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:

  1. GET https://example.com/wp-json/wp/v2/posts/1622/meta/

  2. filtering the above result to get the meta id that I want to change

  3. 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:

  1. This plugin configured to expose your post types and meta
  2. This plugin to enable meta endpoints.
  3. 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).
| improve this answer | |

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