Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aikusuma/844029a7c9d2af4db15364624c4ab86e to your computer and use it in GitHub Desktop.
Save aikusuma/844029a7c9d2af4db15364624c4ab86e to your computer and use it in GitHub Desktop.
Wordpress JSON API Cheat Sheet

Get posts from one category:

http://www.mixmeals.com/wp-json/wp/v2/categories/1

Get posts from one category by slug:

http://www.mixmeals.com/wp-json/wp/v2/categories?slug=lunch

Get posts from multiple categories by slug or id:

http://www.mixmeals.com/wp-json/wp/v2/posts/?filter[category_name]=breakfast,foo

http://www.mixmeals.com/wp-json/wp/v2/posts?categories=2,1

Get page by slug:

http://www.mixmeals.com/wp-json/wp/v2/pages?slug=about-me

Get all posts up to a maximum:

http://www.mixmeals.com/wp-json/wp/v2/posts/?&filter[posts_per_page]=111

Get posts, and include all taxonomy terms and meta fields for that post with _embed:

http://www.mixmeals.com/wp-json/wp/v2/posts/?filter[posts_per_page]=111&_embed

Create a secured, and an unsecured endpoint:

<?php
add_action( 'rest_api_init', 'myhacks_init' );
function myhacks_init() {
	register_rest_route( 'myhacks', '/unsecured', array(
		'methods' => 'GET',
		'callback' => 'myhacks_response',
	) );
	register_rest_route( 'myhacks', '/secured', array(
		'methods' => 'GET',
		'callback' => 'myhacks_response',
		'permission_callback' => 'myhacks_permission_callback',
	) );
}
function myhacks_response() {
	return array( 'doowop' => 'Data wants to be free, yo?' );
}
function myhacks_permission_callback() {
	return current_user_can( 'manage_options' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment