On this page
rest_get_route_for_post( int|WP_Post $post ): string
Gets the REST API route for a post.
Parameters
$postint|WP_Post Required-
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
File: wp-includes/rest-api.php. View all references
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.
Related
Uses
| Uses | Description |
|---|---|
| rest_get_route_for_post_type_items() wp-includes/rest-api.php | Gets the REST API route for a post type. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Server::add_image_to_index() wp-includes/rest-api/class-wp-rest-server.php | Exposes an image through the WordPress REST API. |
| WP_REST_Menu_Items_Controller::prepare_links() wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php | Prepares links for the request. |
| WP_REST_Templates_Controller::prepare_links() wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php | Prepares links for the request. |
| rest_get_queried_resource_route() wp-includes/rest-api.php | Gets the REST route for the currently queried object. |
| WP_REST_Post_Search_Handler::prepare_item_links() wp-includes/rest-api/search/class-wp-rest-post-search-handler.php | Prepares links for the search result of a given ID. |
| WP_REST_Post_Search_Handler::detect_rest_item_route() wp-includes/rest-api/search/class-wp-rest-post-search-handler.php | Attempts to detect the route to access a single item. |
| WP_REST_Revisions_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php | Prepares the revision for the REST response. |
| WP_REST_Posts_Controller::prepare_links() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Prepares links for the request. |
| WP_REST_Posts_Controller::create_item() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Creates a single post. |
| WP_REST_Comments_Controller::prepare_links() wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php | Prepares links for the request. |
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/rest_get_route_for_post