WP_REST_Autosaves_Controller::get_items( WP_REST_Request $request ): WP_REST_Response|WP_Error

Gets a collection of autosaves using wp_get_post_autosave.

Description

Contains the user’s autosave, for empty if it doesn’t exist.

Parameters

$requestWP_REST_Requestrequired
Full details about the request.

Return

WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.

Source

public function get_items( $request ) {
	$parent = $this->get_parent( $request['id'] );
	if ( is_wp_error( $parent ) ) {
		return $parent;
	}

	$response  = array();
	$parent_id = $parent->ID;
	$revisions = wp_get_post_revisions( $parent_id, array( 'check_enabled' => false ) );

	foreach ( $revisions as $revision ) {
		if ( str_contains( $revision->post_name, "{$parent_id}-autosave" ) ) {
			$data       = $this->prepare_item_for_response( $revision, $request );
			$response[] = $this->prepare_response_for_collection( $data );
		}
	}

	return rest_ensure_response( $response );
}

Changelog

VersionDescription
5.0.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.