{"action":"created","issue":{"url":"https://api.github.com/repos/WP-API/WP-API/issues/1238","labels_url":"https://api.github.com/repos/WP-API/WP-API/issues/1238/labels{/name}","comments_url":"https://api.github.com/repos/WP-API/WP-API/issues/1238/comments","events_url":"https://api.github.com/repos/WP-API/WP-API/issues/1238/events","html_url":"https://github.com/WP-API/WP-API/issues/1238","id":76442508,"number":1238,"title":"Custom post type meta not displaying","user":{"login":"joneslloyd","id":997427,"avatar_url":"https://avatars.githubusercontent.com/u/997427?v=3","gravatar_id":"","url":"https://api.github.com/users/joneslloyd","html_url":"https://github.com/joneslloyd","followers_url":"https://api.github.com/users/joneslloyd/followers","following_url":"https://api.github.com/users/joneslloyd/following{/other_user}","gists_url":"https://api.github.com/users/joneslloyd/gists{/gist_id}","starred_url":"https://api.github.com/users/joneslloyd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joneslloyd/subscriptions","organizations_url":"https://api.github.com/users/joneslloyd/orgs","repos_url":"https://api.github.com/users/joneslloyd/repos","events_url":"https://api.github.com/users/joneslloyd/events{/privacy}","received_events_url":"https://api.github.com/users/joneslloyd/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-05-14T18:12:21Z","updated_at":"2015-05-14T19:40:53Z","closed_at":"2015-05-14T18:57:28Z","body":"Hi there,\r\n\r\nI'm using the V2 BETA API, and..\r\n\r\nI've added a custom post type to the API as follows:\r\n\r\n    add_action( 'init', 'add_events_to_json_api' );\r\n    function add_events_to_json_api(){\r\n\t\t\r\n\t\tglobal $wp_post_types;\r\n\t\t$wp_post_types['my_events']->show_in_rest = true;\r\n\t\t$wp_post_types['my_events']->rest_base = 'my_events';\r\n\t\t$wp_post_types['my_events']->rest_controller_class = 'WP_REST_Posts_Controller';\r\n\t}\r\n\r\nAnd then I've tried to expose some meta:\r\n\r\n    add_filter( 'json_prepare_post', 'add_meta_to_json', 10, 3 );\r\n    function add_meta_to_json($data, $post, $context){\r\n\t\t\r\n\t\tif ( $context !== 'view' || is_wp_error( $data ) ) {\r\n\t\t\treturn $data;\r\n\t\t}\r\n\t\t\r\n\t\t$start = '';\r\n\t\t$end = '';\r\n\t\t$location = '';\r\n\t\t\r\n\t\tif(!empty(get_post_meta( $post['ID'], '_my_events_start', true ))){\r\n\t\t\t$start = get_post_meta( $post['ID'], '_my_events_start', true );\r\n\t\t}\r\n\t\t\r\n\t\tif(!empty(get_post_meta( $post['ID'], '_my_events_end', true ))){\r\n\t\t\t$end = get_post_meta( $post['ID'], '_my_events_end', true );\r\n\t\t}\r\n\t\t\r\n\t\tif(!empty(get_post_meta( $post['ID'], '_my_events_location', true ))){\r\n\t\t\t$location = get_post_meta( $post['ID'], '_my_events_location', true );\r\n\t\t}\r\n\t\t\r\n\t\tif($post['post_type'] == 'my_events'){\r\n\t\t\t$data['event_meta'] = array(\r\n\t\t\t\t'start' => $start,\r\n\t\t\t\t'end' => $end,\r\n\t\t\t\t'location' => $location,\r\n\t\t\t);\t\r\n\t\t}\r\n\t\t\r\n\t\treturn $data;\r\n\t}\r\n\r\nHowever, what happens is that when I visit http://mysite.com/wp-json/wp/v2/my_events/6269/\r\nI will get the custom post type, but without the meta I've added above.\r\n\r\nEven removing the post type check in the add_meta_to_json function doesn't help, nor just visiting http://mysite.com/wp-json/wp/v2/posts/6269/\r\n\r\nNot sure if I'm missing something, or if this is an error?\r\n\r\nIf this isn't how I should retrieve meta in the V2 API, is there any chance in letting me know how I should?\r\n\r\nThanks"},"comment":{"url":"https://api.github.com/repos/WP-API/WP-API/issues/comments/102146838","html_url":"https://github.com/WP-API/WP-API/issues/1238#issuecomment-102146838","issue_url":"https://api.github.com/repos/WP-API/WP-API/issues/1238","id":102146838,"user":{"login":"joneslloyd","id":997427,"avatar_url":"https://avatars.githubusercontent.com/u/997427?v=3","gravatar_id":"","url":"https://api.github.com/users/joneslloyd","html_url":"https://github.com/joneslloyd","followers_url":"https://api.github.com/users/joneslloyd/followers","following_url":"https://api.github.com/users/joneslloyd/following{/other_user}","gists_url":"https://api.github.com/users/joneslloyd/gists{/gist_id}","starred_url":"https://api.github.com/users/joneslloyd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joneslloyd/subscriptions","organizations_url":"https://api.github.com/users/joneslloyd/orgs","repos_url":"https://api.github.com/users/joneslloyd/repos","events_url":"https://api.github.com/users/joneslloyd/events{/privacy}","received_events_url":"https://api.github.com/users/joneslloyd/received_events","type":"User","site_admin":false},"created_at":"2015-05-14T19:40:53Z","updated_at":"2015-05-14T19:40:53Z","body":"@danielbachhuber No worries - I can't expect you to look through my code. Thanks for the help though :)\r\n\r\n@rmccue That did it! \r\n\r\nWorking code is:\r\n\r\n    add_filter( 'rest_prepare_my_events', 'add_meta_to_json', 10, 3 );\r\n    function add_meta_to_json($data, $post, $request){\r\n\r\n    $response_data = $data->get_data();\r\n\r\n    if ( $request['context'] !== 'view' || is_wp_error( $data ) ) {\r\n        return $data;\r\n    }\r\n\r\n    $start = '';\r\n    $end = '';\r\n    $location = '';\r\n\r\n    if(!empty(get_post_meta( $post->ID, '_my_events_start', true ))){\r\n        $start = get_post_meta( $post->ID, '_my_events_start', true );\r\n    }\r\n\r\n    if(!empty(get_post_meta( $post->ID, '_my_events_end', true ))){\r\n        $end = get_post_meta( $post->ID, '_my_events_end', true );\r\n    }\r\n\r\n    if(!empty(get_post_meta( $post->ID, '_my_events_location', true ))){\r\n        $location = get_post_meta( $post->ID, '_my_events_location', true );\r\n    }\r\n\r\n    if($post->post_type == 'my_events'){\r\n        $response_data['event_meta'] = array(\r\n            'start' => $start,\r\n            'end' => $end,\r\n            'location' => $location,\r\n        );  \r\n    }\r\n\r\n    $data->set_data( $response_data );\r\n\r\n    return $data;\r\n    }\r\n\r\nThanks a lot! Much appreciated!"}}