WP_REST_Taxonomies_Controller::get_items( WP_REST_Request $request ): WP_REST_Response

In this article

Retrieves all public taxonomies.

Parameters

$requestWP_REST_Requestrequired
Full details about the request.

Return

WP_REST_Response Response object on success, or WP_Error object on failure.

Source

public function get_items( $request ) {

	// Retrieve the list of registered collection query parameters.
	$registered = $this->get_collection_params();

	if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) {
		$taxonomies = get_object_taxonomies( $request['type'], 'objects' );
	} else {
		$taxonomies = get_taxonomies( '', 'objects' );
	}

	$data = array();

	foreach ( $taxonomies as $tax_type => $value ) {
		if ( empty( $value->show_in_rest ) || ( 'edit' === $request['context'] && ! current_user_can( $value->cap->assign_terms ) ) ) {
			continue;
		}

		$tax               = $this->prepare_item_for_response( $value, $request );
		$tax               = $this->prepare_response_for_collection( $tax );
		$data[ $tax_type ] = $tax;
	}

	if ( empty( $data ) ) {
		// Response should still be returned as a JSON object when it is empty.
		$data = (object) $data;
	}

	return rest_ensure_response( $data );
}

Changelog

VersionDescription
4.7.0Introduced.

User Contributed Notes

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