How to Install WordPress REST API to Fetch Posts

by Owais Alam  November 16, 2016

Before we talk about the technical aspects of using WP REST API, it’s important to recall several basic concepts. If you want an in-depth introduction to REST API, you could check the first article of this series Comprehensive Introduction To REST API

API stands for Application Programming Interface. An API is an automatic approach to connect with an application’s data. Facebook’s API, for instance, exposes a host of the features of the Facebook platform.

 

REST is an acronym for Representational State Transfer. An API can be viewed as REST if its design/architecture subscribes to a specific set of constraints including client-server mechanism, independent implementations and options for scalability.

Requests and Responses form complementary components of how HTTP works.

API Client Server

 

  • A client makes a HTTP request to a server 
  • The server responds with an HTTP response.

In HTTP, this mechanism works using GET (Request) and POST (Response).

More About Routes and Endpoints

Note that in the very first example above, we used the following endpoint:

GET wp/v2/posts

Endpoints are capacities that are accessible through the API and they play out a few activities like recovering posts making another user, or updating a post meta. On the other hand, we can say that an endpoint triggers a technique that perform specific task. These endpoints are subject to the HTTP verb connected with them. In the above case, we are utilizing the GET verb to recover all posts.

The route for the above endpoint is the following:

wp/v2/posts

A course is essentially a name to get to the endpoint. A course can have numerous endpoints in view of HTTP verbs, so the above course has the accompanying endpoint to make another post:

POST wp/v2/posts

This endpoint, when triggered with supplied parameters, will create a new post entity.

Consider the following route:

wp/v2/posts/50

This route points to the Post entity having an id of 50. It has the following three endpoints:

  • GET wp/v2/posts/50: Which can be used to retrieve the post having an id of 50. It triggers the get_item() method.
  • PUT wp/v2/posts/50: Can be used to update the post having an id of 50. It triggers the update_item()
  • DELETE wp/v2/posts/50: It deletes the post having an id of 50. It triggers the delete_item() method.

WordPress REST API Using JSON

REST and JSON together give a component to making capable applications utilizing the WordPress back-end. The most prime cases being mobile applications that require the exchange of information between the customer and the server.

Since JSON is a text based format for putting away information, it can be utilized flawlessly with most of the programming languages. Thus JSON serves as a worldwide connector while trading information between various stages that is similarly decipherable by both machines and people.

With the use of API like the one being discussed, the content of your WordPress site is not limited to itself only, but can be accessed by other sites and clients. As API exposes some parts of the internal functionality, remote clients can interact with your site to update or create new content. It also allows to retrieve some content from an existing WordPress site and show it on some other site.

With the utilization of API like the one being examined, the content of your WordPress site is not restricted to itself just, but rather can be gotten to by different destinations and customers. As API uncovered a few sections of the inward usefulness, remote customers can communicate with your site to redesign or make new content. It likewise permits to recover some content  from a current WordPress site and show it on some other site.

Installation Instructions

You should have a solid idea of what the WP API is capable of and what its limitations are. Let’s show you the steps involved in working with it. First off, you need to add the WP API plugin to your WordPress site.

At that point, login to your WordPress site (your-site-name.com/wp-login.php). However plugins in the left sidebar and click on Add New. Click on the Upload Plugin button, click Choose File and select the compressed version of the WP API plugin and afterward click Install Now.Permalink

That’s everything that’s involved with configuring the WP REST API. Now, we can get to the fun stuff and get some data from your WordPress website using the WP API.

Example WP REST API Requests

We’re going to show you examples of some unauthenticated WP API GET requests. So, there’s no need to worry about any authentication plugins or settings.

Start off by adding the Postman Chrome Extension. You could also install the REST Easy Add-On if you use Firefox.

Getting Posts

We’re going to show you how to get all of the posts that currently exist on your WordPress site. In order to do this, copy the following URL (route):

Paste that route into Postman’s Enter ask for URL here field .You could likewise do this by essentially writing the route above in a web browser yet the data won’t be organized so it’ll be more difficult to see the mapping. Replace ‘your-web-site.com’ with the site that you introduced the WP API plugin on. Select GET from the drop down and click ‘Send’.

GET Url

This is sending a GET request to your WordPress site’s server and the server is sending back a response based on the route and the HTTP action that is set. You should see a JSON response similar to the one below.

Fetch

As should be obvious, this responsive is organized positively so you can without much of a stretch and typically get to the information inside it; this is known as a mapping also called schema.  This is structures is really important because knowing it allows you to shift through the data programmatically.

Get a Post

Now that you know how to get a list of the posts on your site, we’ll show you how to get a specific post from your WordPress site with the WP API plugin installed. Copy the route below.

Paste that into the Postman Enter request URL here field. Again, make sure you select GET from the drop down. Replace your-web-site.com with the website that you installed the WP API plugin on AND replace {id} with a Post ID that you know exists on the WordPress site.

GET Post

As you can see, there is no left square bracket to start off the response. This means that this response is an object and NOT an array of objects. Specifically, this response includes all of the post data related to the post with a post ID of 5. The individual post data is in the same format as the list of posts above. You can again parse through the response and see the post id, post title, post content, post excerpt and many other post details.

Conclusion

In this tutorial, I discussed how to utilize WordPress REST API for fetching posts. I also touched upon the installation of JSON on the WordPress website. If you have a question about the topic of this tutorial or would like to contribute to the discussion, please leave a comment below.

Make Your WordPress Website 100% Faster.

Host it now on Cloudways WordPress Hosting Platform.

About Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at owais.alam@cloudways.com

Stay Connected:

You Might Also Like...