WP_REST_Post_Types_Controller::prepare_item_for_response( WP_Post_Type $item, WP_REST_Request $request ): WP_REST_Response

Prepares a post type object for serialization.

Parameters

$itemWP_Post_Typerequired
Post type object.
$requestWP_REST_Requestrequired
Full details about the request.

Return

WP_REST_Response Response object.

Source

public function prepare_item_for_response( $item, $request ) {
	// Restores the more descriptive, specific name for use within this method.
	$post_type = $item;

	$taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) );
	$taxonomies = wp_list_pluck( $taxonomies, 'name' );
	$base       = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
	$namespace  = ! empty( $post_type->rest_namespace ) ? $post_type->rest_namespace : 'wp/v2';
	$supports   = get_all_post_type_supports( $post_type->name );

	$fields = $this->get_fields_for_response( $request );
	$data   = array();

	if ( rest_is_field_included( 'capabilities', $fields ) ) {
		$data['capabilities'] = $post_type->cap;
	}

	if ( rest_is_field_included( 'description', $fields ) ) {
		$data['description'] = $post_type->description;
	}

	if ( rest_is_field_included( 'hierarchical', $fields ) ) {
		$data['hierarchical'] = $post_type->hierarchical;
	}

	if ( rest_is_field_included( 'has_archive', $fields ) ) {
		$data['has_archive'] = $post_type->has_archive;
	}

	if ( rest_is_field_included( 'visibility', $fields ) ) {
		$data['visibility'] = array(
			'show_in_nav_menus' => (bool) $post_type->show_in_nav_menus,
			'show_ui'           => (bool) $post_type->show_ui,
		);
	}

	if ( rest_is_field_included( 'viewable', $fields ) ) {
		$data['viewable'] = is_post_type_viewable( $post_type );
	}

	if ( rest_is_field_included( 'labels', $fields ) ) {
		$data['labels'] = $post_type->labels;
	}

	if ( rest_is_field_included( 'name', $fields ) ) {
		$data['name'] = $post_type->label;
	}

	if ( rest_is_field_included( 'slug', $fields ) ) {
		$data['slug'] = $post_type->name;
	}

	if ( rest_is_field_included( 'icon', $fields ) ) {
		$data['icon'] = $post_type->menu_icon;
	}

	if ( rest_is_field_included( 'supports', $fields ) ) {
		$data['supports'] = $supports;
	}

	if ( rest_is_field_included( 'taxonomies', $fields ) ) {
		$data['taxonomies'] = array_values( $taxonomies );
	}

	if ( rest_is_field_included( 'rest_base', $fields ) ) {
		$data['rest_base'] = $base;
	}

	if ( rest_is_field_included( 'rest_namespace', $fields ) ) {
		$data['rest_namespace'] = $namespace;
	}

	$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
	$data    = $this->add_additional_fields_to_object( $data, $request );
	$data    = $this->filter_response_by_context( $data, $context );

	// Wrap the data in a response object.
	$response = rest_ensure_response( $data );

	if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
		$response->add_links( $this->prepare_links( $post_type ) );
	}

	/**
	 * Filters a post type returned from the REST API.
	 *
	 * Allows modification of the post type data right before it is returned.
	 *
	 * @since 4.7.0
	 *
	 * @param WP_REST_Response $response  The response object.
	 * @param WP_Post_Type     $post_type The original post type object.
	 * @param WP_REST_Request  $request   Request used to generate the response.
	 */
	return apply_filters( 'rest_prepare_post_type', $response, $post_type, $request );
}

Hooks

apply_filters( ‘rest_prepare_post_type’, WP_REST_Response $response, WP_Post_Type $post_type, WP_REST_Request $request )

Filters a post type returned from the REST API.

Changelog

VersionDescription
5.9.0Renamed $post_type to $item to match parent class for PHP 8 named parameter support.
4.7.0Introduced.

User Contributed Notes

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