Title: Adding postmeta to wp-json custom post type
Last modified: April 6, 2019

---

# Adding postmeta to wp-json custom post type

 *  Resolved [snorklebum](https://wordpress.org/support/users/snorklebum/)
 * (@snorklebum)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/adding-postmeta-to-wp-json-custom-post-type/)
 * Hi,
 * I am trying to expose the events manager plugin data via wp-json.
 * So far I have added the following code block to functions.php
 *     ```
       add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 );
   
       function my_post_type_args( $args, $post_type ) {
   
           if ( 'event' === $post_type ) {
               $args['show_in_rest'] = true;
   
               // Optionally customize the rest_base or rest_controller_class
               $args['rest_base']             = 'events';
               $args['rest_controller_class'] = 'WP_REST_Posts_Controller';
           }
   
           return $args;
       }
       ```
   
 * This exposes the events list via wp-json/wp/v2/events/ and individual events 
   with wp-json/wp/v2/events/<id>
 * The events themselves have specific data held in the postsmeta table that I’m
   also looking to expose, ie.
    meta_key _event_start_local, _event_rsvp, etc.
 *     ```
       add_action( 'init', 'register_new_meta');
   
       function register_new_meta() {
            $args = array(
               'type' => 'string',
               'description' => 'Event start date time',
               'single' => true,
               'show_in_rest' => true,
           );
           register_meta( 'post', '_event_start_local', $args );
       }
       ```
   
 * The above changes the post json but changing this to event does not seem to change
   the events json, any advice on what I’m missing would be great.
 * Thanks

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/adding-postmeta-to-wp-json-custom-post-type/#post-11401223)
 * You are conflating post types and object types. All post types (event, page, 
   post, etc.) are all still of the same “post” object type. The first argument 
   for post_meta() is for the related _object type_, not post type. Object types
   relate to which table the data is stored in. The only object types in WP are 
   users, taxonomies, comments, or posts.
 *  Thread Starter [snorklebum](https://wordpress.org/support/users/snorklebum/)
 * (@snorklebum)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/adding-postmeta-to-wp-json-custom-post-type/#post-11401316)
 * Thanks for the reply, apologies if I’ve misunderstood but you seem to be implying
   that the code above is correct(register_meta( ‘post’,..), the events data I am
   trying to expose is in the wp_postmeta table.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/adding-postmeta-to-wp-json-custom-post-type/#post-11404319)
 * “post” IS the correct argument because you are passing an object type. Your event
   post type is still a post object type. When you request your events route when“
   post” is passed to register_meta(), the wp_postmeta data for events should be
   included in the return.
 * If your code is working as you want with “post” passed, the only trouble seems
   to be you wanting to pass “event” instead. That would be incorrect as there is
   no “event” object type in WP.
 *  Thread Starter [snorklebum](https://wordpress.org/support/users/snorklebum/)
 * (@snorklebum)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/adding-postmeta-to-wp-json-custom-post-type/#post-11409111)
 * So I’m using post and when I navigate to wp-json/wp/v2/pages ot wp-json/wp/v2/
   posts I see the meta fields and obviously they are currently “” as I’d expect.
   When I go to wp-json/wp/v2/events the normal entries are show but no meta entry.
   Is there anyway to extend this so that custom types meta is supported?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/adding-postmeta-to-wp-json-custom-post-type/#post-11409340)
 * I tested your code on my site, passing “post” to register_meta() of course. When
   I requested wp-json/wp/v2/events/1242 (the ID of my test event post), the meta
   data is included in the response. IOW, your code works fine. If you are not getting
   post meta back on your site, I have to think it’s related to a theme or plugin
   conflict elsewhere and not due to your code itself.
 *  Thread Starter [snorklebum](https://wordpress.org/support/users/snorklebum/)
 * (@snorklebum)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/adding-postmeta-to-wp-json-custom-post-type/#post-11409521)
 * Well this is frustrating, at the moment I’m testing this on a clean build site
   with twenty-nineteen and events manager is the only plugin.
 * On posts I [see this](https://i.imgur.com/tyiuPHz.png) with the two additional
   fields
 * On events I see the [standard wp meta](https://i.imgur.com/E1iCTIZ.png) but not
   those asked for in register_meta
 * I’ll see if any of the others who have been trying to do the same have had any
   further luck.
 * Thanks for looking in to this with me.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 2 months ago](https://wordpress.org/support/topic/adding-postmeta-to-wp-json-custom-post-type/#post-11413315)
 * It must be something about how the events post type is registered then. I created
   the post type myself using arguments I typically use. I do not have events manager
   active. It’s the only difference between our “clean builds”.
 *  [Timothy Jacobs](https://wordpress.org/support/users/timothyblynjacobs/)
 * (@timothyblynjacobs)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/adding-postmeta-to-wp-json-custom-post-type/#post-11425783)
 * Make sure your post type has `custom-fields` in `supports` [@snorklebum](https://wordpress.org/support/users/snorklebum/).
 * You can also use the `register_post_meta()` introduced in WP 4.9.8 to only register
   the meta key for your post type.
 * `register_post_meta( 'event', 'meta_key', $args );`
 *  Thread Starter [snorklebum](https://wordpress.org/support/users/snorklebum/)
 * (@snorklebum)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/adding-postmeta-to-wp-json-custom-post-type/#post-11438853)
 * [@timothyblynjacobs](https://wordpress.org/support/users/timothyblynjacobs/) 
   Thank you so much, I used post_type_supports() to check for ‘custom-fields’ and
   it was indeed disabled, I believe this is to stop them being editable via edit.
   I’ve just hidden the meta boxes to get around this. Now my fields are appearing
   in the JSON results and I can get on with creating my application 😀
 *  [Andrew Munro / AffiliateWP](https://wordpress.org/support/users/sumobi/)
 * (@sumobi)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/adding-postmeta-to-wp-json-custom-post-type/#post-11465253)
 * Thanks [@timothyblynjacobs](https://wordpress.org/support/users/timothyblynjacobs/)!
   My custom post type also didn’t have support for `custom-fields`. The post meta
   is now being saved correctly. Glad I found this discussion 🙂

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Adding postmeta to wp-json custom post type’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 10 replies
 * 4 participants
 * Last reply from: [Andrew Munro / AffiliateWP](https://wordpress.org/support/users/sumobi/)
 * Last activity: [7 years, 1 month ago](https://wordpress.org/support/topic/adding-postmeta-to-wp-json-custom-post-type/#post-11465253)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
