WordPress JSON Example

Share this article

This article series was rewritten in mid 2017 with up-to-date information and fresh examples.

In this WordPress JSON example, I’ll show how to get a list of posts from a modern WordPress installation. For a better user experience, use Postman to interact with WordPress’ REST API.

WordPress is an open-source Content Management System (CMS) that was launched in 2003. It’s actually a fork from another project known as b2/cafelog that began its roots way back in 2001. Today, WordPress is regarded as the most popular CMS platform and currently powers over 26% of the global web, according to WPManage. It also has about 60% market share, being the most used CMS.

In recent WordPress releases, the REST API was introduced as a built-in feature which opened the door for an endless list of new possibilities. Developers can now write new applications that interact with the site remotely by sending and receiving JSON data. You are no longer constrained to PHP – you are free to use whichever language you are most comfortable with provided it can interact with JSON.

You can launch Postman from this link:
Run in Postman

Once you have launched it, paste this link in the URL field:

https://www.sitepoint.com/wp-json/wp/v2/posts?per_page=3

Make sure GET command is selected. Hit the send button, after a few seconds, you will receive a JSON response. Below is a partial result of what it looks like:

[
  {
      "id": 157538,
      "date": "2017-07-21T10:30:34",
      "date_gmt": "2017-07-21T17:30:34",
      "guid": {
          "rendered": "https://www.sitepoint.com/?p=157538"
      },
      "modified": "2017-07-23T21:56:35",
      "modified_gmt": "2017-07-24T04:56:35",
      "slug": "why-the-iot-threatens-your-wordpress-site-and-how-to-fix-it",
      "status": "publish",
      "type": "post",
      "link": "https://www.sitepoint.com/why-the-iot-threatens-your-wordpress-site-and-how-to-fix-it/",
      "title": {
          "rendered": "Why the IoT Threatens Your WordPress Site (and How to Fix It)"
      },
      "content": {
         ...
      },
      "excerpt": {
          ...
      },
      "author": 72546,
      "featured_media": 157542,
      "comment_status": "open",
      "ping_status": "closed",
      "sticky": false,
      "template": "",
      "format": "standard",
      "meta": [],
      "categories": [
          6132
      ],
      "tags": [
          1798,
          6298
      ],

      }
  ]

I’ve truncated the content and the excerpt to show you a clear overview of the JSON response structure of a WordPress post. To learn more about the WordPress REST API, you should check out this tutorial and this one too.

Here are the other examples in this series:

Frequently Asked Questions (FAQs) about WordPress JSON

What is the WordPress JSON REST API?

The WordPress JSON REST API is a feature of WordPress that provides a standardized interface for interacting with WordPress sites. It allows developers to create, read, update, and delete data from a WordPress site using HTTP requests. This API is built on JSON, a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate.

How do I use the WordPress JSON REST API?

To use the WordPress JSON REST API, you need to send HTTP requests to specific endpoints. Each endpoint corresponds to a specific resource, such as posts, pages, or comments. The type of request (GET, POST, PUT, DELETE) determines the action (read, create, update, delete). You can use any HTTP client to send these requests, including cURL, Postman, or even your web browser.

What are the benefits of using the WordPress JSON REST API?

The WordPress JSON REST API offers several benefits. It allows developers to interact with WordPress data in a standardized, language-agnostic way. This means you can use the API to build applications in any programming language that supports HTTP and JSON. The API also enables developers to create more interactive and dynamic user experiences by fetching and updating data without reloading the page.

How do I authenticate with the WordPress JSON REST API?

Authentication with the WordPress JSON REST API can be done in several ways. The simplest method is cookie authentication, which is suitable for plugins and themes running on the same site. For external applications, you can use OAuth 1.0a or basic authentication with a username and password. However, these methods require additional plugins and are less secure.

Can I customize the WordPress JSON REST API?

Yes, the WordPress JSON REST API is highly customizable. You can add custom endpoints to expose additional data, modify the response of existing endpoints, or even create a completely custom API. This is done using the same hooks and filters that WordPress provides for customizing other aspects of the platform.

What is the JSON Content Importer plugin?

The JSON Content Importer plugin is a WordPress plugin that allows you to import and display data from a JSON feed on your site. You can use it to display data from any JSON API, not just the WordPress REST API. The plugin provides a shortcode that you can use to insert the imported data into your posts or pages.

How do I install the JSON Content Importer plugin?

To install the JSON Content Importer plugin, go to the Plugins section of your WordPress dashboard, click Add New, and search for “JSON Content Importer”. Click Install Now, then Activate. Once activated, you can use the plugin’s shortcode to import and display JSON data.

How do I use the JSON Content Importer plugin?

To use the JSON Content Importer plugin, you need to insert its shortcode into a post or page. The shortcode takes several parameters, including the URL of the JSON feed, the template for displaying the data, and optional parameters for controlling the cache and error handling.

What is the difference between the WordPress JSON REST API and the JSON Content Importer plugin?

The WordPress JSON REST API and the JSON Content Importer plugin serve different purposes. The API allows you to interact with WordPress data using HTTP requests, while the plugin allows you to import and display data from any JSON feed on your site. You can use the plugin to display data from the API, but they are not the same thing.

Can I use the WordPress JSON REST API to build a mobile app?

Yes, you can use the WordPress JSON REST API to build a mobile app. The API provides a standardized interface for interacting with WordPress data, which you can use to fetch posts, pages, comments, and other data for your app. You can also use the API to create, update, and delete data from your app.

Michael WanyoikeMichael Wanyoike
View Author

I write clean, readable and modular code. I love learning new technologies that bring efficiencies and increased productivity to my workflow.

jsonnilsonjWordPress API
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week