List of all default Rest API endpoints in WordPress website

In WordPress there are lot of default rest API endpoints which may help you while development.

How to access rest API endpoints?

You can access rest API endpoints though URLs. E.g. http://wpdigits.com/wp-json/wp/v2/posts

Above URL show the list of all posts in the form of JSON.

In this example endpoint, Domain name is http://wpdigits.com/ and the /wp-json/ shows all the default endpoints.


Below is the list of all default rest API endpoints from any WordPress fresh setup.

/wp/v2/posts,
/wp/v2/posts/(?P<id>[\d]+),
/wp/v2/posts/(?P<parent>[\d]+)/revisions,
/wp/v2/posts/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+),
/wp/v2/posts/(?P<id>[\d]+)/autosaves,
/wp/v2/posts/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+),
/wp/v2/pages,
/wp/v2/pages/(?P<id>[\d]+),
/wp/v2/pages/(?P<parent>[\d]+)/revisions,
/wp/v2/pages/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+),
/wp/v2/pages/(?P<id>[\d]+)/autosaves,
/wp/v2/pages/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+),
/wp/v2/media,
/wp/v2/media/(?P<id>[\d]+),
/wp/v2/blocks,
/wp/v2/blocks/(?P<id>[\d]+),
/wp/v2/blocks/(?P<id>[\d]+)/autosaves,
/wp/v2/blocks/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+),
/wp/v2/types,
/wp/v2/types/(?P<type>[\w-]+),
/wp/v2/statuses,
/wp/v2/statuses/(?P<status>[\w-]+),
/wp/v2/taxonomies,
/wp/v2/taxonomies/(?P<taxonomy>[\w-]+),
/wp/v2/categories,
/wp/v2/categories/(?P<id>[\d]+),
/wp/v2/tags,
/wp/v2/tags/(?P<id>[\d]+),
/wp/v2/users,
/wp/v2/users/(?P<id>[\d]+),
/wp/v2/users/me,
/wp/v2/comments,
/wp/v2/comments/(?P<id>[\d]+),
/wp/v2/search,
/wp/v2/block-renderer/(?P<name>core/block),
/wp/v2/block-renderer/(?P<name>core/latest-comments),
/wp/v2/block-renderer/(?P<name>core/archives),
/wp/v2/block-renderer/(?P<name>core/categories),
/wp/v2/block-renderer/(?P<name>core/latest-posts),
/wp/v2/block-renderer/(?P<name>core/shortcode),
/wp/v2/settings,
/wp/v2/theme

Create the post on wordpress.com site using oAuth and Rest API

Working on Rest API and OAuth is very interesting.

WordPress merged Rest API support into the core in WordPress 4.7 “Vaughan” check out the Rest API Merge Proposal.

In this post, I’m creating the post on my site https://maheshwaghmare.wordpress.com/ though rest API and OAuth.

Let’s Try it Step by Step

Step 1:

Create new app wordpress.com app from https://developer.wordpress.com/apps/new/

OR

Do you already have an app then select existing app from https://developer.wordpress.com/apps/

Step 2:

Copy below code and add your app authentication details. After adding details you get and auth key.

// Step: 1  Add authentication details to get auth key.
$auth_args = array(
	'username'      => '',
	'password'      => '',
	'client_id'     => '',
	'client_secret' => '',
	'grant_type'    => 'password', // Keep this as it is.
);
$access_key = get_access_key( $auth_args );

Note: Keep grant_type as password. Its required!

  • username – Your website username.
  • password – Your website password.
  • client_id – Your App client ID.
  • client_id – Your App client ID.

Step 3:

Use below function which return the auth key from your given app details.

/**
 * Get Access Key.
 * 
 * @param  array $args 	Auth arguments.
 * @return mixed       	Auth response.
 */
function get_access_key( $args ) {

	// Access Token.
	$curl = curl_init( 'https://public-api.wordpress.com/oauth2/token' );
	curl_setopt( $curl, CURLOPT_POST, true );
	curl_setopt( $curl, CURLOPT_POSTFIELDS, $args );
	curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
	$auth = curl_exec( $curl );
	$auth = json_decode($auth);

	// Access Key.
	return $auth->access_token;
}

Step 4:

Add your new post details. Such as post title, post content etc. like below:

// Step: 2  Set post arguments and pass it create the post.
$post_args = array(
	'title'       => 'Hello APA World',
	'content'     => 'Hello. I am a test post. I was created by the API',
	'tags'        => 'tests',
	'post_status' => 'draft',
	'categories'  => 'API',
);

Step 5:

Now, Simply call a function and pass the Auth key and post arguments.

// Create a post with the access key.
create_post( $access_key, $post_args );

Step 6:

/**
 * Create post with access key.
 * 
 * @param  string $access_key 	Access key.
 * @param  array $post_args 	Post arguments.
 * @return mixed       			Post response.
 */
function create_post( $access_key, $post_args )
{
	$options  = array (
		'http' => array(
			'ignore_errors' => true,
			'method'        => 'POST',
			'header'        => array(
		    	0 => 'authorization: Bearer ' . $access_key,
		    	1 => 'Content-Type: application/x-www-form-urlencoded',
		  	),
		  	'content' => http_build_query( $post_args ),
		),
	);
	 
	$context  = stream_context_create( $options );
	$response = file_get_contents(
	    'https://public-api.wordpress.com/rest/v1/sites/YOURSITEID/posts/new/',
	    false,
	    $context
	);
	return json_decode( $response );
}

Here,

https://public-api.wordpress.com/rest/v1/sites/YOURSITEID/posts/new/

Replace YOURSITEID with your wordpress.com site ID.

To get it follow below quick simple steps:

  • Goto api console
  • Add string /me/sites and select method GET
  • Press Enter.

It’ll show all your sites from WordPress.com. Check the below screenshot for reference.


Step 7:

Yup! Its not a step.

That’s it. Your first post successfully created!

Use below Complete Code snippet.Try this and let me know in comments.

The complete code snippet to create a post on wordpress.com is below:

Conclusion

In above example you can create a post on wordpress.com with the help of get_access_key() and create_post() functions from above code snippet.

Note: use unique prefix for get_access_key and create_post() to avoid the conflict with existing function with same name.

Working with Rest API is pretty simple. Let’s try it and let me know in comments.

WordPress Rest API query parameters mappings

This array defines mappings between public API query parameters whose values are accepted as-passed, and their internal WP_Query parameter name equivalents (some are the same). Only values which are also present in registered will be set.

Below is the list of all the endpoints which have query parameters mapping:

  • wp/v2/comments
  • wp/v2/<post-type>
  • wp/v2/<taxonomy>
  • wp/v2/<term>
  • wp/v2/<users>

Check below list of parameters mapping by Rest API endpoints:

wp/v2/comments

$parameter_mappings = array(
    'author'         => 'author__in',
    'author_email'   => 'author_email',
    'author_exclude' => 'author__not_in',
    'exclude'        => 'comment__not_in',
    'include'        => 'comment__in',
    'offset'         => 'offset',
    'order'          => 'order',
    'parent'         => 'parent__in',
    'parent_exclude' => 'parent__not_in',
    'per_page'       => 'number',
    'post'           => 'post__in',
    'search'         => 'search',
    'status'         => 'status',
    'type'           => 'type',
);

wp/v2/<post-type>

$parameter_mappings = array(
    'author'         => 'author__in',
    'author_exclude' => 'author__not_in',
    'exclude'        => 'post__not_in',
    'include'        => 'post__in',
    'menu_order'     => 'menu_order',
    'offset'         => 'offset',
    'order'          => 'order',
    'orderby'        => 'orderby',
    'page'           => 'paged',
    'parent'         => 'post_parent__in',
    'parent_exclude' => 'post_parent__not_in',
    'search'         => 's',
    'slug'           => 'post_name__in',
    'status'         => 'post_status',
);

wp/v2/<taxonomy>

$parameter_mappings = array(
    'exclude'    => 'exclude',
    'include'    => 'include',
    'order'      => 'order',
    'orderby'    => 'orderby',
    'post'       => 'post',
    'hide_empty' => 'hide_empty',
    'per_page'   => 'number',
    'search'     => 'search',
    'slug'       => 'slug',
);

wp/v2/<term>

       
$parameter_mappings = array(
    'exclude'    => 'exclude',
    'include'    => 'include',
    'order'      => 'order',
    'orderby'    => 'orderby',
    'post'       => 'post',
    'hide_empty' => 'hide_empty',
    'per_page'   => 'number',
    'search'     => 'search',
    'slug'       => 'slug',
);

wp/v2/<users>

$parameter_mappings = array(
    'exclude'  => 'exclude',
    'include'  => 'include',
    'order'    => 'order',
    'per_page' => 'number',
    'search'   => 'search',
    'roles'    => 'role__in',
    'slug'     => 'nicename__in',
);

WordPress Rest API Get specific posts by IDs by using `include` parameter

With the Rest API we get all the posts. But, Sometimes we want to get only specific posts by post ids. WordPress have rest API parameter include which return only requested posts by post ids.

Syntax:
https://example.org/wp-json/wp/v2/posts?include=<post-ids&gt;

Example 1:
https://example.org/wp-json/wp/v2/posts?include=11,12
It return only 2 posts whose ids are 11 & 12.

Note: By default it return 10 posts. If you have more than 10 posts then add parameter `per_page` which return all the posts.

Example 2:
https://example.org/wp-json/wp/v2/posts/?include=18177,18178,18198,18258,18137,18138,18143,18144,18167,18166,18314,18322,18131,18135,18145,18156,18159,18158,18165,18169,18183,18185,18199,18227,18253,18262,18263,18283,18312,18325,18330,18329,18332,18333,18140,18152,18163,18180,18193,18191,18195,18200,18204,18205,18214,18218,18219,18221,18224,18242,18252,18273,18286,18288,18307&amp;per_page=100

Add post meta in post (custom post type) endpoint with Rest API

Add post meta in post (custom post type) endpoint with Rest API.

Todo:

  • Change post-type with your post type slug.
  • Change prefix-meta-key with your post meta key which you want to include in Rest API Response.

Output:

Visit https://<mysite>/wp-json/wp/v2/<post-type>/ it show the stored data from meta key  prefix-meta-key in the Rest API response.

Code: