WP_REST_Posts_Controller::get_post

Advertisement

Summery Summery

Get the post, if the ID is valid.

Syntax Syntax

WP_REST_Posts_Controller::get_post( int $id )

Parameters Parameters

$id

(Required) Supplied ID.

Return Return

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

Source Source

File: wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

	protected function get_post( $id ) {
		$error = new WP_Error(
			'rest_post_invalid_id',
			__( 'Invalid post ID.' ),
			array( 'status' => 404 )
		);

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

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

		return $post;
	}

Advertisement

Changelog Changelog

Changelog
Version Description
4.7.2 Introduced.

Advertisement

Leave a Reply