rest_get_route_for_post() WordPress Function

The rest_get_route_for_post() function is used to retrieve the REST route for a given post. This function is typically used to generate the URL for a post when it is being accessed via the REST API.

rest_get_route_for_post( int|WP_Post $post ) #

Gets the REST API route for a post.


Parameters

$post

(int|WP_Post)(Required)Post ID or post object.


Top ↑

Return

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


Top ↑

Source

File: wp-includes/rest-api.php

function rest_get_route_for_post( $post ) {
	$post = get_post( $post );

	if ( ! $post instanceof WP_Post ) {
		return '';
	}

	$post_type_route = rest_get_route_for_post_type_items( $post->post_type );
	if ( ! $post_type_route ) {
		return '';
	}

	$route = sprintf( '%s/%d', $post_type_route, $post->ID );

	/**
	 * Filters the REST API route for a post.
	 *
	 * @since 5.5.0
	 *
	 * @param string  $route The route path.
	 * @param WP_Post $post  The post object.
	 */
	return apply_filters( 'rest_route_for_post', $route, $post );
}


Top ↑

Changelog

Changelog
VersionDescription
5.5.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