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
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.
All metadata keys can 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 (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.
Let us know if you have any questions in the comments below.