Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now
2 added note about authentication
source | link

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

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.

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).
1
source | link

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.