How do I get the intended post type of a revision post?

I have a headless Wordpress instance with multiple custom post types. The build takes a few minutes, so I'm building a way to display drafts by fetching the latest revision via an API, like so:

$revision = wp_get_post_revision($id);
wp_send_json($revision);

I retrieve the revision just fine, but the post type is set to revision instead of my custom post type. This causes problems, because I need the intended post type in order to build up a valid response that I can parse on the front-end.

How can I obtain the intended post type of a revision post?

Topic headless revisions rest-api api custom-post-types Wordpress

Category Web


Post revisions have the original post as their parent, so you can get the post type by checking the post type of the revision's parent:

$revision  = wp_get_post_revision( $id );
$post_type = get_post_type( $revision->post_parent );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.