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!

Filed under: Blog, Web Development, What I LearnedTagged with: ,

4 Comments

  1. I know this may sound stupid, but I have to share this. Part of the whole ordeals you get whilst developing where you just laugh at something so bad. I accidentally placed the REST-API code on the WP install that requests the info… [face_i_suck] and spent a day trying to figure out why it didn’t work… hahaaha.

    • Hahaha, I’ve been there, Marco. I’m glad you got it figured out!

  2. Excellent and very useful. However, I do hope that in a future release the post meta fields, user meta fields and importantly, the advanced custom fields, are all exposed, so they can all be read and updated in one api.

    • Hey Chris! Yeah, I hear that a lot from folks. I think I think the main thing about meta is that it can be used to store anything, and some folks might just start using the custom fields stuff as a dump for “all the extra stuff”… I think the folks who put the API together kinda said “We don’t know what could be in there, so we don’t want to expose it by default.” That’s purely my speculation :-). And by “using the custom fields”, I mean the default fields that ship with wp-core, plus anything that gets associated with the post that’s not in the wp_posts table by default… Again, my speculation. Have a good one!


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *

Name *
Email *
Website