We’ve recently made some updates to the REST API which is available here on WordPress.com and for any Jetpack-enabled site that have 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 it’s 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
You can also query posts by
metadata using the new
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
operations, 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.
By default, all metadata keys are allowed in the API, as long as they are not
protected keys. Any metadata key that starts with
_ is by default protected. Protected metadata keys can, however, be accessed and edited by users with the
edit_post_meta (used for editing and viewing),
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 by any user, even if that key is protected.
Here’s a quick rundown. If the user performing the request is unauthenicated, he or she will not be able to add, edit or delete any metadata; will be able to read any metadata for which the key doesn’t begin with
_ but will not be able to read any metadata which begins with
_ unless it is whitelisted.
If the user performing the request is authenticated and has the above listed capabilities, he or she, will be able to read, add, edit or delete any metadata, even if it begins with
_ and 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 in 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.