WP_REST_Global_Styles_Controller::get_post()protectedWP 5.9.0

Get the post, if the ID is valid.

Method of the class: WP_REST_Global_Styles_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_post( $id );
$id(int) (required)
Supplied ID.

Changelog

Since 5.9.0 Introduced.

WP_REST_Global_Styles_Controller::get_post() code WP 6.6.2

protected function get_post( $id ) {
	$error = new WP_Error(
		'rest_global_styles_not_found',
		__( 'No global styles config exist with that id.' ),
		array( 'status' => 404 )
	);

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

	$post = get_post( $id );
	if ( empty( $post ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
		return $error;
	}

	return $post;
}