WP_REST_Revisions_Controller::get_parent()protectedWP 4.7.2

Get the parent post, if the ID is valid.

Method of the class: WP_REST_Revisions_Controller{}

No Hooks.

Return

WP_Post|WP_Error. Post object if ID is valid, WP_Error otherwise.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_parent( $parent_post_id );
$parent_post_id(int) (required)
Supplied ID.

Changelog

Since 4.7.2 Introduced.

WP_REST_Revisions_Controller::get_parent() code WP 6.5.2

protected function get_parent( $parent_post_id ) {
	$error = new WP_Error(
		'rest_post_invalid_parent',
		__( 'Invalid post parent ID.' ),
		array( 'status' => 404 )
	);

	if ( (int) $parent_post_id <= 0 ) {
		return $error;
	}

	$parent_post = get_post( (int) $parent_post_id );

	if ( empty( $parent_post ) || empty( $parent_post->ID )
		|| $this->parent_post_type !== $parent_post->post_type
	) {
		return $error;
	}

	return $parent_post;
}