TL;DR: How to choose every bit of information of the response of a WP REST API custom endpoint?
LONG VERSION
If I want to build a custom endpoint with the WP REST API - sending specific post data from different post types - following the example in the Handbook, I got this:
function custom_endpoint ( $data ) {
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => array('event', 'post'),
) );
if ( empty( $posts ) ) {
return null;
}
return $posts;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v1', '/custom-endpoint/', array(
'methods' => 'GET',
'callback' => 'custom_endpoint',
) );
} );
But the get_post() function doesn't return some piece of data that is very useful if you wish to display posts in your page(category id, featured image, for intance). So how can I build a custom endpoint that returns:
- Post Title
- Post Date
- Post Author
- Post Excerpt
- Post Content
- Post Featured Image (like Better Featured Images plugin)
- Post Category
- Post Type
- Post Link
- Other usfeul informations