rest_get_route_for_post( int|WP_Post $post ): string

In this article

Gets the REST API route for a post.

Parameters

$postint|WP_Postrequired
Post ID or post object.

Return

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

Source

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 );
}

Hooks

apply_filters( ‘rest_route_for_post’, string $route, WP_Post $post )

Filters the REST API route for a post.

Changelog

VersionDescription
5.5.0Introduced.

User Contributed Notes

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