We’ve recently made some updates to the REST API which is available here on WordPress.com and for any Jetpack-enabled site that has the REST API module activated. The API now has full read and write support for custom post types and post metadata.
You can specify a post’s post type when you create or edit it. If you’re fetching a single post, you will receive its post type in the response. Of course, you can also specify a post type when fetching a series of posts. In all cases the parameter to use or look out for is type
.
You can also query posts by metadata
using the new meta_key
and meta_value
parameters. You can add, update, delete or retrieve a post’s metadata when creating, editing or getting a single post, using the new metadata
parameter which accepts an array of metadata key
s, id
s, previous_value
s, value
s and operation
s, based on the operation you are trying to perform.
In order to enable your custom post types to work with the REST API on your Jetpack enabled sites, you will need to whitelist them using the new rest_api_allowed_post_types
filter available in Jetpack, which is a simple array
of the post type names that should be allowed to work with the API.
All metadata keys can be accessed and edited by users with the edit_post_meta
(used for editing and viewing), add_post_meta
and delete_post_meta
capabilities as appropriate for each operation. We’ve also added a filter rest_api_allowed_public_metadata
that allows you to specifically whitelist certain metadata keys to be accessed (but not modified) by any user (even anonymous).
Here’s a quick rundown. If the user performing the request is unauthenticated, they will not be able to add, edit or delete any metadata; they will only be able to read specifically whitelisted metadata keys.
If the user performing the request is authenticated and has the above listed capabilities, they will be able to read, add, edit or delete any metadata, even if it is not whitelisted.
In order to make it easier for you to add support for custom post types and metadata in your own plugins and themes, we’ve also added a third filter jetpack_rest_api_compat
in the main Jetpack file, which allows you to load additional files where you can then add all of your REST API compatibility code, using the filters above.
Because we love bbPress, we’ve bundled support for bbPress and the REST API directly into Jetpack, so that you can read, write and edit bbPress forums, topics and replies right out of the box. You can also use this file as an example on how to bundle support in your own plugins or themes.
Check out our documentation, create an app, and get started today!
Let us know if you have any questions in the comments below.
Reblogged this on Joey Kudish and commented:
An internal tool that I’m building at Automattic is using the WordPress.com REST API. As part of that tool, I needed access to custom post types and post metadata, so I’ve added both to our REST API. And now anyone on a Jetpack-powered site can also use this part of the REST API. Find out the details in this developer blog post.
Reblogged this on Jetpack for WordPress.
I hope that you will add support for Custom post types for Publicize, Photon and other modules… JetPack is huge help, but for CMS use of WP not so much… Enchanted support for Custom post types in JetPack would be dream come true for me and many other developers / users. .
Having the ability to integrate Photon to custom post type will be great news.
Reblogged this on John James Jacoby and commented:
This is super cool…
I really like these latest enhancements 🙂
One major thing I’d really like to see though, as a plugin developer super eager to start doing some cool things here:
meta_query / tax_query support in get posts (single site / multiple sites)
Please keep that in mind for the future, so much that we can do once we’ve got that, would save a lot of manual iteration through PHP arrays to filter down.
I agree it would be cool to see meta_query / tax_query in the API. No plans for it as of now as far as I know, though we do support basic meta_key, meta_value and tags and categories queries.
I’ve updated the corresponding status on the Pods integration with Jetpack here:
https://github.com/pods-framework/pods/issues/953
The original ticket was supposed to cover custom endpoints, but that was a no go since plugins can’t create their own right now.
Also, it wasn’t immediate apparent, but in the get post data, does it return data unserialized or do serialized arrays remain serialized?
And in add/update post data, assuming it accepts arrays/objects and will serialize accordingly?
An array/object should be returned as a json array or object. And yes the API should accept arrays and objects. If you have any trouble with it, please let us know!
Reblogged this on Josh Betz.
Thanks for the documentation link, some more reading and tinkering for the weekend.
Is there a filter to add meta keys to a blacklist? Say you have meta that is intended to be edited only by users with ‘unfiltered_html’ capabilities, not ‘edit_meta_data’. Anyway to disable or modify the api exposure?
Hi Ken,
Sorry for the delayed response. At this time, it’s not possible to blacklist an API key. Can you please share a bit more details about what you’re doing and we might be able to help out better?
I see now that the edit_meta_data capability isn’t assigned to any users unless explicitly assigned to the key. I misunderstood its implementation. I was worried that a key would be exposed unintentionally. Thanks!
As a content type / custom field plugin developer, it would also be useful to be able to control access to specific keys via this API based on the role of the API user.
I’ll work on adding a new filter so that you can do this in a future version of Jetpack and the API. Stay tuned!
Reblogged this on haliil24.
Hi I tried to access my api unauthenticated and all I got in the metadata array is [false], according to this article, I can access my post metadata without authentication, any idea? Thanks!
Here is an example of how to add the `rest_api_allowed_post_types` filter: https://gist.github.com/brendannee/7196251
Reblogged this on oppseekblok and commented:
simple explaination. easy understanding
Is there any support for custom taxonomies such as custom categories and custom tags?
At this time, there is not.
Is it possible to add custom taxonomies? I used rest_api_allowed_post_types to add my custom post type, but cannot find a way to add my custom taxonomy.
This is on our roadmap though it’s not currently possible.
How can I fetch custom taxonomy?
Unfortunately at this time the REST API doesn’t support custom taxonomies. We have plans to add support at some time in the future but nothing to announce as of now.
Nice article!
Was looking for jetpack json api request, response structure for android app at hackpundit.com.
Also wanted custom fields to be shown in json for lightweight response. Description was not required
Thanks!
Have you guys made any progress on custom taxonomy support for REST API?
No progress to report at the moment but do stay tuned as we’re going to be making more updates in the future!