How to get posts from an external site using the WP-REST API

On a recent project, my client had several microsites where he wanted to display the same blog posts from one central site. So I decided to simply use the WordPress REST API to do this.

Accessing the WordPress REST API

I’m not going to go into details of what the WP REST API is but to access it simply add /wp-json/wp/v2/posts to the end of any WordPress site URL. This will give you a list of posts in JSON format.

So this is the code I used: [gist /]

I appended two parameters to the API URL: embedded and posts_per_page.
The embedded parameter allows me to access the post featured image amongst other things. posts_per_page is simply the number of posts I want to be returned.

There are other arguments you can append to get specific posts.

Feel free to play with the code.