I am trying to slim down the JSON returned when using the Rest API. I am able to get it to work using the "_embed" parameter in the query, but it returns a HUGE amount of data I don't need. So, per WP best practices I want to set up a custom end point and in it's callback call a function to output the three items I need. Seems simple, however it returns NULL for all nodes when I try. In my functions.php file I have:
function get_all_posts( WP_REST_Request $request ) {
return [
'id' => $data->data['id'],
'title' => $data->data['title']['rendered'],
'link' => $data->data['link'],
'date' => $data->data['date'], //not correct
//similar calls to get thumbnail image and category
];
}
Then the route and call back for the custom endpoint
add_action( 'rest_api_init', function () {
register_rest_route( 'mydata/v1', '/all', array(
'methods' => 'GET',
'callback' => 'get_all_posts',
) );
} );
when I use the url - https://somedomain.com/blog/wp-json/mydata/v1/all I get the following on page:
{
"id": null,
"title": null,
"link": null
}