• Email: [email protected]
  • Dedicated Server Login
  • About
  • 9288041031
  • Knowledge Base
  • Support
India's best web hosting | Ehostpiya India
  • Home
  • Hosting
    Web Hosting Quality Hosting Starting at ₹60/mo
    Cloud VPS Starting at ₹300/mo
    Dedicated Server First Starting at ₹2401/mo
    Reseller Web Hosting Conveniently Starting at ₹421/mo
    Application For Hosting
    • iconWordPress Hosting
    • iconJoomla Hosting
    • iconMagento Hosting
    • iconOpencart Hosting
    • iconPrestashop Hosting
    • iconDrupal Hosting
  • Email
  • Domain
  • SVG
    Contact Us Face any problem contact with us
    SVG
    About Us We are leading hosting company
  • My Account
Web Hosting Blogs by Ehostpiya | Tips and Tricks Web Hosting Blogs by Ehostpiya | Tips and Tricks
Buy Now

Archives

  • October 2026
  • September 2026
  • August 2026
  • October 2023

Categories

  • Control Panels
  • Magento
  • Miscellaneous
  • SEO
  • Technology
  • Trends
  • Web Hosting
  • Wordpress
Facebook Twitter Instagram Pinterest
Home How to Use the WordPress REST API?
ByVandeeta
October 1, 2026

How to Use the WordPress REST API?

Table of Contents

Toggle
  • How to Use the WordPress REST API
    • What Is the WordPress REST API?
  • Why Use the WordPress REST API?
  • Understanding REST API Endpoints
  • How to Access the WordPress REST API
  • What Is JSON?
  • Common WordPress REST API Endpoints
      • Posts
      • Pages
      • Categories
      • Tags
      • Media
  • How to Retrieve a Specific Post
  • How to Retrieve Posts With Parameters
  • Search WordPress Posts Using the REST API
  • Retrieve Posts by Category
  • Retrieve Posts by Tag
  • Sort REST API Results
  • Using the REST API With JavaScript
  • Display WordPress Posts With JavaScript
  • Using the WordPress REST API With PHP
  • Understanding Authentication
  • WordPress Application Passwords
  • Making an Authenticated Request
  • Create a WordPress Post With the REST API
  • Update an Existing Post
  • Delete a Post
  • Working With Media
  • WordPress REST API Pagination
  • Create a Custom REST API Endpoint
  • Why Use API Namespaces?
  • Add Permission Checks to Custom Endpoints
  • Validate Custom REST API Parameters
  • REST API and Headless WordPress
  • REST API and Custom Post Types
  • How to Check Whether the REST API Is Working
      • Browser
      • Command Line
      • JavaScript
      • API Testing Tools
  • Common WordPress REST API Errors
    • 404 Not Found
    • 401 Unauthorized
    • 403 Forbidden
    • 400 Bad Request
  • REST API Security Best Practices
      • Use HTTPS
      • Protect Credentials
      • Use Least Privilege
      • Validate Input
      • Escape Output
      • Add Permission Checks
      • Avoid Exposing Sensitive Information
      • Monitor API Activity
  • WordPress REST API Performance Tips
  • REST API vs WordPress Admin
  • Best Practices for Using the WordPress REST API
  • Frequently Asked Questions
    • Is the WordPress REST API enabled by default?
    • What URL is used for the WordPress REST API?
    • Can I create WordPress posts using the REST API?
    • Can I use the REST API with JavaScript?
    • Can I build a mobile application with the WordPress REST API?
    • Can custom post types use the REST API?
    • Is the WordPress REST API secure?
  • Final Thoughts

How to Use the WordPress REST API

The WordPress REST API allows applications and services to communicate with WordPress using standard HTTP requests. Instead of interacting with WordPress only through the traditional dashboard, developers can retrieve, create, update, and delete WordPress data programmatically.

This makes WordPress much more flexible. A WordPress website can provide content to mobile applications, JavaScript frontends, external services, custom dashboards, and other websites.

In this guide, you’ll learn what the WordPress REST API is, how to access API endpoints, retrieve posts, work with pages and users, make authenticated requests, create custom endpoints, and troubleshoot common REST API problems.


What Is the WordPress REST API?

The WordPress REST API is an interface that allows external applications to interact with WordPress data through HTTP requests.

For example, a request such as:

https://example.com/wp-json/wp/v2/posts

can return WordPress posts in JSON format.

The API uses common HTTP methods:

HTTP MethodTypical Purpose
GETRetrieve data
POSTCreate data
PUTReplace data
PATCHUpdate data
DELETEDelete data

WordPress primarily uses REST routes and endpoints to determine which data or functionality a request should access.


Why Use the WordPress REST API?

The REST API is useful when WordPress needs to communicate with another application.

Common use cases include:

  • Mobile applications
  • React applications
  • Vue.js websites
  • Next.js frontends
  • Custom dashboards
  • Headless WordPress websites
  • External content systems
  • Automated publishing tools
  • Third-party integrations
  • JavaScript-powered interfaces
  • Custom WordPress plugins

For example, a developer could use WordPress as a content management system while displaying the content through a separate frontend application.


Understanding REST API Endpoints

An API endpoint is a URL that provides access to a particular resource.

A standard WordPress posts endpoint looks like:

/wp-json/wp/v2/posts

For a website located at:

https://example.com

the complete endpoint becomes:

https://example.com/wp-json/wp/v2/posts

WordPress returns the requested information as JSON.


How to Access the WordPress REST API

The easiest way to test the REST API is to open an endpoint in your browser.

For example:

https://example.com/wp-json/wp/v2/posts

Replace example.com with your WordPress domain.

If the REST API is available, WordPress will return JSON data containing posts.

A response may look similar to:

[
  {
    "id": 101,
    "date": "2026-09-30T10:00:00",
    "slug": "example-post",
    "status": "publish",
    "type": "post",
    "title": {
      "rendered": "Example Post"
    }
  }
]

The actual response contains considerably more information.


What Is JSON?

JSON stands for JavaScript Object Notation.

It is a lightweight format commonly used for exchanging data between applications.

For example:

{
  "id": 101,
  "title": "My WordPress Post",
  "status": "publish"
}

A JavaScript application can read this information and display it on a webpage.


Common WordPress REST API Endpoints

WordPress provides many built-in REST API routes.

Some commonly used endpoints include:

/wp-json/wp/v2/posts
/wp-json/wp/v2/pages
/wp-json/wp/v2/media
/wp-json/wp/v2/categories
/wp-json/wp/v2/tags
/wp-json/wp/v2/users

For example:

Posts

https://example.com/wp-json/wp/v2/posts

Pages

https://example.com/wp-json/wp/v2/pages

Categories

https://example.com/wp-json/wp/v2/categories

Tags

https://example.com/wp-json/wp/v2/tags

Media

https://example.com/wp-json/wp/v2/media

Some endpoints require authentication or appropriate permissions.


How to Retrieve a Specific Post

If you know a post’s ID, you can request it directly.

For example:

https://example.com/wp-json/wp/v2/posts/101

The final number represents the post ID.

The API returns information about that specific post.


How to Retrieve Posts With Parameters

WordPress REST API endpoints support query parameters.

For example:

/wp-json/wp/v2/posts?per_page=5

This requests up to five posts.

You can also request posts from a particular page:

/wp-json/wp/v2/posts?page=2&per_page=10

This is useful when building pagination into an application.


Search WordPress Posts Using the REST API

You can search posts using the search parameter:

/wp-json/wp/v2/posts?search=hosting

This can return posts matching the search term.

For example:

https://example.com/wp-json/wp/v2/posts?search=hosting

This functionality can be useful for creating custom search interfaces.


Retrieve Posts by Category

The REST API can filter posts by category.

For example:

/wp-json/wp/v2/posts?categories=5

Here, 5 represents the category ID.

You can combine multiple parameters:

/wp-json/wp/v2/posts?categories=5&per_page=10

Retrieve Posts by Tag

You can similarly filter posts using a tag ID:

/wp-json/wp/v2/posts?tags=12

You can combine filters with pagination and ordering.


Sort REST API Results

The REST API provides parameters for ordering results.

For example:

/wp-json/wp/v2/posts?orderby=date&order=desc

This requests posts ordered by date in descending order.

Other supported ordering options depend on the endpoint.


Using the REST API With JavaScript

One of the most common uses of the WordPress REST API is retrieving WordPress content with JavaScript.

For example:

fetch('https://example.com/wp-json/wp/v2/posts')
  .then(response => response.json())
  .then(posts => {
    console.log(posts);
  })
  .catch(error => {
    console.error('API Error:', error);
  });

The fetch() function sends the HTTP request.

The response is then converted into JSON.


Display WordPress Posts With JavaScript

You can also use the returned data to generate HTML.

Example:

fetch('https://example.com/wp-json/wp/v2/posts?per_page=5')
  .then(response => response.json())
  .then(posts => {

    const container = document.getElementById('posts');

    posts.forEach(post => {
      const article = document.createElement('article');

      article.innerHTML = `
        <h2>${post.title.rendered}</h2>
        <div>${post.excerpt.rendered}</div>
      `;

      container.appendChild(article);
    });

  });

HTML:

<div id="posts"></div>

This creates a simple JavaScript-powered WordPress post listing.

When inserting API-provided HTML into a page, make sure the data source is trusted and consider appropriate sanitization and rendering practices.


Using the WordPress REST API With PHP

PHP applications can also request REST API data.

For example:

$response = wp_remote_get(
    'https://example.com/wp-json/wp/v2/posts'
);

if (is_wp_error($response)) {
    return;
}

$body = wp_remote_retrieve_body($response);

$posts = json_decode($body, true);

WordPress’s HTTP API provides functions such as wp_remote_get() for making HTTP requests.

You can then process the returned data in PHP.


Understanding Authentication

Public WordPress content can often be retrieved without authentication.

However, operations that modify WordPress data generally require authentication and appropriate permissions.

For example:

  • Creating posts
  • Updating posts
  • Deleting posts
  • Uploading media
  • Modifying settings
  • Accessing protected information

WordPress must be able to determine who is making the request and whether that user is authorized to perform the requested operation.


WordPress Application Passwords

For external applications that need to authenticate with a WordPress site, Application Passwords provide a built-in authentication mechanism for supported WordPress installations.

Application Passwords are different from a user’s normal WordPress login password.

They are intended for programmatic access.

In WordPress, an administrator or eligible user can manage application passwords from the user’s profile area.

An application password should be treated like a credential and should never be published in JavaScript, Git repositories, screenshots, or public code.


Making an Authenticated Request

For example, a PHP application can use Basic Authentication with an application password over HTTPS.

A simplified example is:

$username = 'api-user';
$application_password = 'xxxx xxxx xxxx xxxx xxxx xxxx';

$response = wp_remote_post(
    'https://example.com/wp-json/wp/v2/posts',
    array(
        'headers' => array(
            'Authorization' => 'Basic ' . base64_encode(
                $username . ':' . $application_password
            ),
            'Content-Type' => 'application/json',
        ),
        'body' => wp_json_encode(
            array(
                'title'   => 'New API Post',
                'content' => 'This post was created through the REST API.',
                'status'  => 'draft',
            )
        ),
    )
);

Always use HTTPS when sending credentials.

Never place real credentials directly into publicly accessible frontend JavaScript.


Create a WordPress Post With the REST API

An authenticated POST request can create a new post.

The request is sent to:

/wp-json/wp/v2/posts

The request body might contain:

{
  "title": "My New Post",
  "content": "This content was created through the REST API.",
  "status": "draft"
}

The status can be set according to the permissions and workflow required.

For example:

draft

can be useful when content should be reviewed before publication.


Update an Existing Post

To update an existing post, send an authenticated request to its endpoint.

For example:

/wp-json/wp/v2/posts/101

A request body could contain:

{
  "title": "Updated Post Title"
}

The server updates the post if the authenticated user has sufficient permissions.


Delete a Post

An authenticated DELETE request can be used to remove a post.

For example:

/wp-json/wp/v2/posts/101

with the HTTP method:

DELETE

Some WordPress resources provide additional options for moving content to the trash rather than permanently deleting it.

Always test deletion operations carefully.


Working With Media

The REST API also provides a media endpoint:

/wp-json/wp/v2/media

This can be used to retrieve media library items and, with appropriate authentication, upload media.

Media can then be associated with posts.

For example, a custom publishing application could:

  1. Upload an image.
  2. Receive the media ID.
  3. Create a post.
  4. Assign the image as the featured media.
  5. Publish or save the post.

This is useful for automated publishing workflows.


WordPress REST API Pagination

REST API responses are paginated.

For example:

/wp-json/wp/v2/posts?per_page=10&page=2

requests the second page of results.

WordPress also provides pagination information through response headers.

Common headers include:

X-WP-Total
X-WP-TotalPages

These can help applications determine how many results and pages are available.


Create a Custom REST API Endpoint

WordPress allows developers to register their own REST API routes.

A simple example:

function ehostpiya_register_api_route() {

    register_rest_route(
        'ehostpiya/v1',
        '/message',
        array(
            'methods'  => 'GET',
            'callback' => 'ehostpiya_api_message',
        )
    );
}

add_action('rest_api_init', 'ehostpiya_register_api_route');

function ehostpiya_api_message() {

    return array(
        'message' => 'Hello from the custom REST API endpoint.',
    );
}

The endpoint becomes:

/wp-json/ehostpiya/v1/message

A request might look like:

https://example.com/wp-json/ehostpiya/v1/message

The API could return:

{
  "message": "Hello from the custom REST API endpoint."
}

Why Use API Namespaces?

In this example:

ehostpiya/v1

is the namespace.

Namespaces generally contain:

  • A unique project or plugin identifier
  • An API version

For example:

myplugin/v1

When the API changes significantly, you can introduce another version:

myplugin/v2

Versioning can help maintain compatibility for applications using the older API.


Add Permission Checks to Custom Endpoints

Custom endpoints should not automatically expose sensitive functionality.

For example:

'permission_callback' => function () {
    return current_user_can('manage_options');
},

A complete example:

function ehostpiya_register_private_route() {

    register_rest_route(
        'ehostpiya/v1',
        '/settings',
        array(
            'methods'  => 'GET',
            'callback' => 'ehostpiya_get_settings',
            'permission_callback' => function () {
                return current_user_can('manage_options');
            },
        )
    );
}

add_action('rest_api_init', 'ehostpiya_register_private_route');

The permission callback determines who can access the endpoint.

Avoid exposing private information through publicly accessible API routes.


Validate Custom REST API Parameters

Custom endpoints may accept parameters.

For example:

'args' => array(
    'id' => array(
        'required' => true,
        'validate_callback' => function ($value) {
            return is_numeric($value);
        },
    ),
),

Validation helps ensure that incoming values meet the expected requirements before processing them.


REST API and Headless WordPress

The REST API makes it possible to use WordPress as a headless CMS.

In a traditional WordPress installation:

WordPress
   ↓
WordPress Theme
   ↓
Website

With a headless architecture:

WordPress
   ↓
REST API
   ↓
React / Vue / Next.js / Mobile App
   ↓
Frontend

WordPress manages the content while another application controls the presentation layer.


REST API and Custom Post Types

Custom post types can also be exposed through the REST API.

When registering a custom post type, developers can enable REST API support using:

'show_in_rest' => true,

For example:

register_post_type(
    'services',
    array(
        'public'       => true,
        'show_in_rest' => true,
        'label'        => 'Services',
    )
);

The custom post type can then be available through a REST endpoint such as:

/wp-json/wp/v2/services

The exact route depends on the post type’s configuration.


How to Check Whether the REST API Is Working

You can test the API using:

Browser

Open:

https://example.com/wp-json/

Command Line

Using cURL:

curl https://example.com/wp-json/wp/v2/posts

JavaScript

fetch('https://example.com/wp-json/wp/v2/posts')
    .then(response => response.json())
    .then(data => console.log(data));

API Testing Tools

Tools designed for HTTP API testing can also be used to inspect:

  • Request methods
  • Headers
  • Authentication
  • Parameters
  • JSON responses
  • HTTP status codes

Common WordPress REST API Errors

404 Not Found

A 404 response can occur when:

  • The endpoint is incorrect.
  • The route does not exist.
  • A custom endpoint was registered incorrectly.
  • Rewrite rules need to be refreshed.
  • A security or server configuration is interfering.

Check the endpoint carefully.


401 Unauthorized

A 401 response generally indicates that authentication is missing or invalid.

Check:

  • Username
  • Application Password
  • Authorization header
  • HTTPS configuration
  • Authentication method

403 Forbidden

A 403 response commonly indicates that the request is understood but the current user does not have permission to perform the operation.

Check the user’s capabilities and the endpoint’s permission rules.


400 Bad Request

A 400 response can indicate invalid request data.

Check:

  • JSON formatting
  • Required parameters
  • Parameter values
  • Request headers
  • Endpoint requirements

REST API Security Best Practices

The REST API should be treated like any other application interface.

Follow these practices:

Use HTTPS

Never send authentication credentials over an unencrypted HTTP connection.

Protect Credentials

Do not place application passwords inside public JavaScript.

Use Least Privilege

Give API users only the permissions they actually need.

Validate Input

Validate parameters before processing them.

Escape Output

Escape dynamic data when it is rendered into HTML or another context.

Add Permission Checks

Custom endpoints should use appropriate permission_callback logic.

Avoid Exposing Sensitive Information

Do not return passwords, private tokens, internal configuration values, or unnecessary personal information through public endpoints.

Monitor API Activity

If your website receives significant API traffic, monitor server logs and application behavior for unusual requests.


WordPress REST API Performance Tips

API requests can consume server resources, particularly when applications repeatedly request large datasets.

Consider the following practices:

  • Request only the data you need.
  • Use pagination.
  • Limit per_page values.
  • Avoid unnecessary repeated requests.
  • Cache appropriate API responses.
  • Optimize database queries in custom endpoints.
  • Avoid loading unnecessary plugins or data.
  • Use efficient frontend request patterns.

For example, instead of retrieving hundreds of posts at once:

/wp-json/wp/v2/posts?per_page=100

consider retrieving smaller pages:

/wp-json/wp/v2/posts?per_page=10&page=1

and then requesting additional pages when necessary.


REST API vs WordPress Admin

The WordPress REST API and WordPress admin dashboard serve different purposes.

FeatureWordPress AdminREST API
Human interfaceYesNo
Programmatic accessLimitedYes
JSON responsesNot the primary interfaceYes
Mobile applicationsNot designed for thisSuitable
JavaScript applicationsLimitedExcellent
Automated integrationsLimitedExcellent
Content managementYesYes, through API requests

The REST API does not replace the WordPress dashboard for every situation. Instead, it provides another way for applications to communicate with WordPress.


Best Practices for Using the WordPress REST API

Keep these recommendations in mind:

  1. Use HTTPS for API communication.
  2. Authenticate requests that modify data.
  3. Protect application passwords.
  4. Use unique namespaces for custom APIs.
  5. Version custom API endpoints.
  6. Add permission callbacks to protected routes.
  7. Validate incoming parameters.
  8. Sanitize data where appropriate.
  9. Escape output according to its context.
  10. Use pagination for large datasets.
  11. Avoid unnecessary API requests.
  12. Cache data when appropriate.
  13. Return only the information an application needs.
  14. Test custom endpoints before deploying them.
  15. Monitor server resources when API traffic is high.

Frequently Asked Questions

Is the WordPress REST API enabled by default?

Modern WordPress installations generally expose the standard REST API functionality by default, although individual endpoints, authentication requirements, plugins, security configurations, or server settings can affect access.

What URL is used for the WordPress REST API?

The standard API base is:

/wp-json/

For example:

https://example.com/wp-json/

Can I create WordPress posts using the REST API?

Yes. An authenticated request with sufficient permissions can create posts through the appropriate REST API endpoint.

Can I use the REST API with JavaScript?

Yes. JavaScript applications commonly use fetch() or other HTTP clients to retrieve WordPress data.

Can I build a mobile application with the WordPress REST API?

Yes. A mobile application can communicate with WordPress through REST API endpoints, provided authentication, permissions, and API requirements are handled correctly.

Can custom post types use the REST API?

Yes. Custom post types can be exposed through the REST API by configuring them appropriately, including show_in_rest => true.

Is the WordPress REST API secure?

The REST API itself provides mechanisms for authentication and authorization, but security depends heavily on how endpoints, credentials, permissions, input validation, and custom code are implemented.


Final Thoughts

The WordPress REST API turns WordPress into a flexible content platform that can communicate with external applications through HTTP and JSON.

For simple projects, you can start by retrieving posts from:

/wp-json/wp/v2/posts

As your requirements become more advanced, you can use authentication to create and modify content, integrate WordPress with JavaScript applications, expose custom post types, and build your own REST API endpoints.

When developing custom API functionality, security should always be a priority. Use HTTPS, protect credentials, validate input, implement permission checks, and return only the data that an application actually needs.

With these practices, the WordPress REST API can become a powerful foundation for integrations, headless websites, mobile applications, automation tools, and custom WordPress solutions.

This article can also be expanded into a more advanced follow-up covering how to create custom WordPress REST API endpoints with GET, POST, authentication, validation, and database queries.

ByVandeeta
Updated October 01, 2026
  • Wordpress

Read Next

ByVandeeta
October 1, 2026

How to Create a WordPress Staging Website?

Here’s a unique, SEO-friendly tutorial suitable for a WordPress/web-hosting knowledge base. How to Create a WordPress Staging Website…
  • Wordpress
ByVandeeta
October 1, 2026

How to Clone a WordPress Website

How to Clone a WordPress Website Cloning a WordPress website means creating a complete copy of an existing…
  • Wordpress
ByVandeeta
October 1, 2026

How to Set Up WordPress Multisite?

How to Set Up WordPress Multisite WordPress Multisite allows you to run multiple WordPress websites from a single…
  • Wordpress

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Explore Categories

  • Control Panels 7
  • Magento 52
  • Miscellaneous 5
  • SEO 17
  • Technology 10
  • Trends 9
  • Web Hosting 7
  • Wordpress 100

Recent Posts

  • How to Fix WordPress “Too Many Redirects”
  • How to Fix WordPress Critical Error
  • How to Fix WordPress Database Connection Error
  • How to Fix WordPress White Screen of Death
  • How to Fix WordPress 404 Error

Recent Comments

  1. Tabias H. on The Future is 5G: Understanding the Next Wave of Connectivity
  2. Elliot Alderson on Evolving Workspaces: The Rise of the Hybrid Work Trend
  3. James Mann on Evolving Workspaces: The Rise of the Hybrid Work Trend
Swyft

Welcome to the Ehostpiya Blog, your trusted resource for web hosting, WordPress, VPS, domains, website security, and online business tips.

Our goal is to make web hosting and website management easier to understand for everyone—from beginners launching their first website to experienced developers and business owners. We publish practical guides, hosting tips, tutorials, and helpful insights to help you build, manage, and grow your website successfully.

At Ehostpiya, we believe that reliable information and quality hosting go hand in hand. Explore our latest articles and discover simple solutions for a faster, safer, and more successful online presence.

  • Facebook
  • Twitter
  • Instagram
  • Pinteres

Featured Posts

  • AI and Ethics: Navigating the Complex Landscape of Artificial Intelligence
    by Vandeeta
    October 11, 2023
  • AR and VR: Blurring the Lines Between Reality and Virtuality
    by Vandeeta
    October 11, 2023
  • Best WordPress Caching Plugins to Improve Website Speed
    by Vandeeta
    September 26, 2026
  • Blockchain Unchained: How Distributed Ledger Technology is Transforming Industries
    by Vandeeta
    October 11, 2023

Web Hosting Discount

cLAIM NOW
logo

Ehostpiya is reliable Web hosting provider, with aim to provide affordable Website and application hosting to various clients for their online business and presence.

Hosting
  • Web Hosting
  • Cloud VPS
  • Reseller Hosting
  • Email Hosting
  • Dedicated Server
  • Nodejs Hosting
  • SSL Certificate
Legal
  • Accepted Uses Policy
  • privacy Policy
  • Terms of Service
  • Refund and cancellation
  • Service Delivery Policy
Company
  • About Us
  • Our Clients
  • Contact Us
  • Our Blog
Support & Tools
  • Open Support Request
  • View Ticket
  • Knowledge Base
  • Report Abuse
  • check nameserver
  • What is my IP
  • Mx lookup

© Ehostpiya, All rights reserved

  • Terms & Condition
  • Privacy Policy