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?

  • Which version of WordPress do you use? As of 4.7 the REST API plugin merged into the core and there have been major changes. – Laxmana Feb 24 '17 at 15:45
  • yeah, I am on 4.7.2, and there is a shocking lack of documentation anywhere on the web. Wordpress' own documentation in this area is terrible, alas. – Stephen Feb 24 '17 at 15:47
  • and the fact that I am on 4.7.2 is why I needed some plugins to restore functionality they took out. :/ – Stephen Feb 24 '17 at 15:48
  • Yeah, the docs are very poor for the moment, especially with the new changes. Sorry but I can't help you more. Have you try to make an UPDATE request instead of POST? – Laxmana Feb 24 '17 at 15:56
  • as far as I know, UPDATE is not a supported method in the API – Stephen Feb 24 '17 at 16:04
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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