Getting Started with the WP REST API

Posted in WordPress on the 21st of November 2016 by @aaronrutley

Last week at the Melbourne WordPress Developer Meetup I gave a presentation on ‘Getting Started with the WordPress REST API’ here’s a follow-up post about it.

The purpose of this post is to introduce the WP REST API and to explain some practical ways developers can get started using it in their projects!

For anyone that’s not familiar with the WordPress REST API, it’s essentially an alternative way for developers to communicate with the WordPress database.

It’s been under development for the past few years, RyanRachel & the team have put in a huge amount of work. It’s exciting to see the content endpoints will be in WordPress version 4.7 due for release in early December 2016.

The Acronyms

  • JSON – JavaScript Object Notation
  • REST – Representational State Transfer
  • API – Application Programming Interface

TLDR; It’s a developer friendly, well-structured way to talk to the WP database.

The REST API is all about Endpoints

The endpoint to GET the latest posts from my blog is:

If you wanted to GET page 2 of my blog you could hit this endpoint:

The endpoint to GET the details the details of post 3556 would be

You can explore all endpoints / routes / resources here:

Please note – I’m using a bespoke page builder so not all of my content is displayed right now.

Posting data to the REST API

The endpoint to POST a new page would be:

The endpoint to PUT data to update a page with an id of 4 would be:

Please note – this would require authentication which is covered in the WP REST API docs.

The default Content Endpoints

In WordPress 4.7 we’ll have endpoints for the following

  • Posts / Pages / Media
  • Comments
  • Terms
  • Users
  • Meta

You can read more about the content endpoints over on the make.wordpress.org blog.

Endpoints for Custom Post Types

By default, Custom Post Types are not exposed in the REST API. However, you can easily change that by defining 'show_in_rest' => true, wherever you registered your custom post type.

Here’s an example of a custom post type endpoint on a site I created recently called AwesomeACF:

Here’s an example of a CPT endpoint that queries just the latest 10 extensions:

Custom Meta & Custom Endpoints

It’s possible to include custom meta data in the API response for any post type, this is ideal for exposing select data you might have registered with Advanced Custom Fields, you can use this gist to get you started.

It’s also possible to create your own endpoints, for the AwesomeACF site I wanted an endpoint that listed all 100+ items & no other data, here’s the gist and here’s the endpoint:

Simple ways to use the WP REST API

Load latest news from PostStatus.com

See the Pen WP REST API Demo #1 by Aaron Rutley (@aaronrutley) on CodePen.

Show the latest 10 extensions added to AwesomeACF.com

See the Pen WP REST API Demo #2 by Aaron Rutley (@aaronrutley) on CodePen.

Tips for getting started with the REST API

In summary, why is the REST API a big deal ?

  • It can be used right now to improve your existing projects!
  • It can be used in core/plugins to improve the wp-admin (eg: Quick Draft)
  • With authentication, we can use it to create Custom Dashboards
  • We can now use WordPress as Headless CMS (Data store for an iOS App)
  • It can be used to create Single Page Applications (React, Angular, Vue)

Share this post