src/Post.php
public function endpoint_callback( \WP_REST_Request $request ) {
		$params = $request->get_params();

		$id = $params['id'];

		$slug = false === $params['slug'] ? false : trim( $params['slug'], '/' );

		if ( false === $id && false === $slug ) {
			return new \WP_Error( self::INVALID_PARAMS, 'The request must have either an id or a slug', [ 'status' => 400 ] );
		}

		$query_args = [
			'post_type' => 'any',
			'no_found_rows' => true,
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
		];

		if ( false !== $id ) {
			$query_args['p'] = $id;
		} else {
			$query_args['name'] = $slug;
		}

		$query = new \WP_Query( apply_filters( $this->get_query_filter_name(), $query_args, $request ) );

		if ( $query->have_posts() ) {
			$query->the_post();

			$post = $query->post;

			$data = [
				'id' => $post->ID,
				'slug' => $post->post_name,
				'type' => Type::get( $post ),
				'content' => Content::get( $post ),
				'meta' => Meta\Post::get_all_post_meta( $post ),
			];

			wp_reset_postdata();

			return $this->filter_data( $data, $post->ID );
		}

		return new \WP_Error( self::NOT_FOUND, 'Nothing found for this query', [ 'status' => 404 ] );
	}
public function endpoint_callback( \WP_REST_Request $request ) {
		$params = $request->get_params();

		$id = $params['id'];

		$slug = false === $params['slug'] ? false : trim( $params['slug'], '/' );

		if ( false === $id && false === $slug ) {
			return new \WP_Error( self::INVALID_PARAMS, 'The request must have either an id or a slug', [ 'status' => 400 ] );
		}

		$query_args = [
			'post_type' => 'any',
			'no_found_rows' => true,
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
		];

		if ( false !== $id ) {
			$query_args['p'] = $id;
		} else {
			$query_args['name'] = $slug;
		}

		$query = new \WP_Query( apply_filters( $this->get_query_filter_name(), $query_args, $request ) );

		if ( $query->have_posts() ) {
			$query->the_post();

			$post = $query->post;

			$data = [
				'id' => $post->ID,
				'slug' => $post->post_name,
				'type' => Type::get( $post ),
				'content' => Content::get( $post ),
				'meta' => Meta\Post::get_all_post_meta( $post ),
			];

			wp_reset_postdata();

			return $this->filter_data( $data, $post->ID );
		}

		return new \WP_Error( self::NOT_FOUND, 'Nothing found for this query', [ 'status' => 404 ] );
	}
public function endpoint_callback ( \ WP_REST_Request $ request ) { $ params = $ request -> get_params ( ) ; $ id = $ params [ 'id' ] ; $ slug = false === $ params [ 'slug' ] ? false : trim ( $ params [ 'slug' ] , '/' ) ; if ( false === $ id && false === $ slug ) { return new \ WP_Error ( self :: INVALID_PARAMS , 'The request must have either an id or a slug' , [ 'status' => 400 ] ) ; } $ query_args = [ 'post_type' => 'any' , 'no_found_rows' => true , 'update_post_meta_cache' => false , 'update_post_term_cache' => false , ] ; if ( false !== $ id ) { $ query_args [ 'p' ] = $ id ; } else { $ query_args [ 'name' ] = $ slug ; } $ query = new \ WP_Query ( apply_filters ( $ this -> get_query_filter_name ( ) , $ query_args , $ request ) ) ; if ( $ query -> have_posts ( ) ) { $ query -> the_post ( ) ; $ post = $ query -> post ; $ data = [ 'id' => $ post -> ID , 'slug' => $ post -> post_name , 'type' => Type :: get ( $ post ) , 'content' => Content :: get ( $ post ) , 'meta' => Meta \ Post :: get_all_post_meta ( $ post ) , ] ; wp_reset_postdata ( ) ; return $ this -> filter_data ( $ data , $ post -> ID ) ; } return new \ WP_Error ( self :: NOT_FOUND , 'Nothing found for this query' , [ 'status' => 404 ] ) ; }
Get the post.

@param \WP_REST_Request $request The request.

@return array|\WP_Error
Get the post .