Get started with 33% off your first certification using code: 33OFFNEW

Retrieving Private Posts from WordPress API

2 min read
Published on 13th July 2023

The WordPress REST API provides an interface to interact with your WordPress site from external clients. In this tutorial, we will focus on how to retrieve private posts using authentication.

Prerequisites

Before you begin, ensure you have a WordPress website set up, and you are logged in as an administrator.

Step 1: Enable Basic Authentication

To authenticate ourselves when making API requests, we will use Basic Authentication. There is a plugin available to easily enable this in WordPress called the Basic Auth plugin. Download and install this plugin to your WordPress site. Remember, you should only use Basic Auth over HTTPS, as credentials are transmitted in plain text.

We rarely recommend the use of plugins, but this plugin is created by the official WP API team and can be treated as an extension of functionality to the WP API itself.

Step 2: Use Postman for API Requests

Postman is a robust tool for testing APIs. Download and install Postman on your machine.

Step 3: Setup Basic Authentication in Postman

Open Postman, create a new request, and select GET as the request method. Then, input your API endpoint URL.

The URL structure for the WordPress API is https://yourwebsite.com/wp-json/wp/v2/posts. If you want to retrieve private posts, append ?status=private to the URL, like so: https://yourwebsite.com/wp-json/wp/v2/posts?status=private.

Next, set up Basic Authentication:

  1. In the Authorization tab, choose Basic Auth from the TYPE dropdown.
  2. Input your WordPress username and password.

Step 4: Send the Request

Click on the Send button to send the request. If everything is set up correctly, the response will include your private posts in JSON format.

Step 5: Parsing the Response

The response from the API is in JSON format. Each post is represented as an object in the array. Here is an example of what a post might look like:

{
  "id": 1,
  "date": "2021-09-09T11:21:49",
  "slug": "hello-world",
  "status": "private",
  "title": {
    "rendered": "Hello world!"
  },
  "content": {
    "rendered": "Welcome to WordPress. This is your first post."
  },
  ...
}

You can use the built-in JSON viewer in Postman to navigate through this data.

There you have it, you should now be able to pull private posts from the WordPress API using authentication.