What filters will transform a normal WP_Post
object (as returned from get_posts()
) to match the post objects returned from native REST API endpoints?
I have a custom endpoint which returns a WP_Post
object, but rest_ensure_response
just returns a simple JSON dump of the post:
function endpoint() {
$p = get_posts(['include' => 10]);
return rest_ensure_response($p);
}
// wp-json/custom/v1/example
[{ "ID": 10, "post_date": "2017-01-23 16:15:13", "post_content": "<p>..." }]
Returning the same post natively from the REST API remaps the data to something like this (keys are lowercase, dates are normalized, content is rendered):
// wp-json/wp/v2/posts/10
[{ "id": 10, "date": "2017-01-23T16:15:13", "content": {"rendered": "<p>..."}}]
How can I make the posts from my custom endpoint match those from native endpoints? Clearly the functions/filters exist, manually recreating them would be silly.