Skip to content

Instantly share code, notes, and snippets.

@zonay
Created February 20, 2020 19:25
Show Gist options
  • Save zonay/a29805f2719ea1b8d93a17fe27ad6e97 to your computer and use it in GitHub Desktop.
Save zonay/a29805f2719ea1b8d93a17fe27ad6e97 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:

'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