Getting Post Meta for the WP REST API

Getting Post Meta for the WP REST API

My friend and I are working on a little side project. It’s a job posting site for an industry we’re familiar with: ESL jobs around the world. While this project could be seen as “just another job board”, we’re hoping to add some features that will make it fun to use and valuable for schools and recruiters. I of course want to build it on WordPress, but to get a some of the functionality I want, I need to expose some post meta to the WP REST API.

In researching how to do this, I quickly found out that the WP REST API doesn’t naturally expose post or user meta. As with most things WP REST API, you don’t have to go too far down the rabbit hole before you find a practical article written by the Josh Pollock. While the register_api_field function isn’t in use anymore, the register_rest_field function is, so this article and that function were all I needed to get going.

Here’s the code for getting all post meta for the WP REST API:

add_action( 'rest_api_init', 'create_api_posts_meta_field' );

function create_api_posts_meta_field() {

 // register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
 register_rest_field( 'post', 'post-meta-fields', array(
 'get_callback' => 'get_post_meta_for_api',
 'schema' => null,
 )
 );
}

function get_post_meta_for_api( $object ) {
 //get the id of the post object array
 $post_id = $object['id'];

 //return the post meta
 return get_post_meta( $post_id );
}

The comments in there are self explanatory, but this also allows me to get the custom fields for any custom post type as well (Josh’s example is a jedi custom post type, which is way cooler than my practical job-postings custom post type 🙂 ). That way, I can save whatever information in the custom fields of my job-postings custom post type I need, like:

  • application deadline
  • salary
  • location
  • type of teaching job
  • whatever else I need!

While it currently exposes all post meta, I can definitely filter it as I need to to remove anything extra. However, here’s the basic post meta dump in a post endpoint:

Hopefully, if you’re struggling on how to get this done, this snippet will help and you can extend it how you like.

Cheers!

 

===

UPDATE Feb. 2, 2018

Josh Pollock recently wrote a new article reviewing the register_meta() function. This function has gotten some updates and love, and now allows for you add an array of arguments, one of which is 'show_in_rest' => true. This allows the meta to be shown as children of the meta node (empty in the above screenshot). That’s pretty nice! The original version of this will still work, and there are some cases where you should still use it.

Josh outlines different use cases and how to use the new array of arguments here. There’s also a link in that article to an article by Pippin Williamson about using custom table meta data in the REST API. Enjoy!

Up Next

2017: Harder, Better, Faster, Stronger

2017: Harder, Better, Faster, Stronger