WP REST API (WP API), Tips and Tricks

Published:

I’m starting to use the WP API more and more, and have definitely needed some help along the way. ย Here are some tips and tricks I’ve been able to pick up and use.

  • Get multiple types of posts / pages / etc
    • /wp-json/posts?type[]=post-type&type[]=post-type
  • Filter by specific number of posts per page
    • /wp-json/posts?filter[posts_per_page]=4&page=2 (number of posts you want, and which page)
  • Get a random post
    • /wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1
  • Get One post at a time (pull in first post in posts / pages/ post-type)
    • /wp-json/posts?filter[posts_per_page]=1
    • /wp-json/posts?type=post-type&filter[posts_per_page]=1
  • Get ALL the posts
    • /wp-json/posts?filter[posts_per_page]=-1
  • Get a specific post type post by ID or post by ID
    • /wp-json/posts?type=post-type&filter[page_id]=69420
    • /wp-json/posts?filter[page_id]=69420
  • Get posts in reverse alphabetical order (for shits and giggles)
    • /wp-json/posts?filter[order]=DESC
  • Get posts after a specific date (could be better I’m sure)
    • /wp-json/posts?filter[date_query][after]=2014-09-15T15:48:52

I’ll be adding more as I go along and come across ones that I believe would help