Skip to content

Instantly share code, notes, and snippets.

@verticalgrain
Last active November 23, 2023 10:00
Show Gist options
  • Save verticalgrain/eb694cfbc8ac7da7ae8d876858019921 to your computer and use it in GitHub Desktop.
Save verticalgrain/eb694cfbc8ac7da7ae8d876858019921 to your computer and use it in GitHub Desktop.
Wordpress JSON API Cheat Sheet

POSTS:

Get a post by id:

http://mixmeals.com/wp-json/wp/v2/posts/?filter[p]=12

Get multiple posts by id:

http://mixmeals.com/wp-json/wp/v2/posts/?include=470,469

Get a post by slug:

http://mixmeals.com/wp-json/wp/v2/posts?slug=easy-stuffed-cabbage-rolls-with-quinoa

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 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

Search posts by keyword:

http://www.mixmeals.com/wp-json/wp/v2/posts?=search[keyword]

PAGES:

Get page by slug:

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

USERS:

Get a User by User ID:

http://www.mixmeals.com/wp-json/wp/v2/users/4567

MEDIA:

Get a featured media item by ID:

http://www.mixmeals.com/wp-json/wp/v2/media/44498

CUSTOM ENDPOINTS:

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