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)
Viewing 10 replies - 1 through 10 (of 10 total)
- The topic ‘Adding postmeta to wp-json custom post type’ is closed to new replies.