rest_get_route_for_post_type_items() WordPress Function

The rest_get_route_for_post_type_items() function is used to generate the REST API route for retrieving a post type's items. This function is used internally by the Wordpress REST API to generate the proper API route.

rest_get_route_for_post_type_items( string $post_type ) #

Gets the REST API route for a post type.


Parameters

$post_type

(string)(Required)The name of a registered post type.


Top ↑

Return

(string) The route path with a leading slash for the given post type, or an empty string if there is not a route.


Top ↑

Source

File: wp-includes/rest-api.php

function rest_get_route_for_post_type_items( $post_type ) {
	$post_type = get_post_type_object( $post_type );
	if ( ! $post_type ) {
		return '';
	}

	if ( ! $post_type->show_in_rest ) {
		return '';
	}

	$namespace = ! empty( $post_type->rest_namespace ) ? $post_type->rest_namespace : 'wp/v2';
	$rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
	$route     = sprintf( '/%s/%s', $namespace, $rest_base );

	/**
	 * Filters the REST API route for a post type.
	 *
	 * @since 5.9.0
	 *
	 * @param string       $route      The route path.
	 * @param WP_Post_Type $post_type  The post type object.
	 */
	return apply_filters( 'rest_route_for_post_type_items', $route, $post_type );
}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More