WP_REST_Posts_Controller::delete_item( WP_REST_Request $request ): WP_REST_Response|WP_Error
- Since
- 4.7.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:1090
Compatibility
- WordPress
- since 4.7.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$requestWP_REST_Request- Full details about the request.
Return value
WP_REST_Response|WP_Error- Response object on success, or WP_Error object on failure.
Performance profile
How much work a call to WP_REST_Posts_Controller::delete_item() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 12–73
- Plugin surface
- 2 hooks
- Called by
- 2
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 134.
Third-party callbacks on 'rest_{$this->post_type}_trashable', 'rest_delete_{$this->post_type}' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_post()called directly
Further down the call graph this can also reach cache, transient, serialize and option. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Posts_Controller::delete_item() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 12 | ->get_post(), is_wp_error() |
!is_wp_error() && !->check_delete_permission() | 45–49 | ->get_post(), is_wp_error(), apply_filters(), ->check_delete_permission(), __(), rest_authorization_required_code(), status() |
!is_wp_error() && ->check_delete_permission() | 51–55 | ->get_post(), is_wp_error(), apply_filters(), ->check_delete_permission(), ->set_param(), __() |
!is_wp_error() && ->check_delete_permission() | 52–56 | ->get_post(), is_wp_error(), apply_filters(), ->check_delete_permission(), ->set_param(), __(), sprintf(), ->set_data() |
!is_wp_error() && ->check_delete_permission() | 65–69 | ->get_post(), is_wp_error(), apply_filters(), ->check_delete_permission(), ->set_param(), wp_trash_post(), get_post(), ->prepare_item_for_response(), do_action() |
!is_wp_error() && ->check_delete_permission() | 65–69 | ->get_post(), is_wp_error(), apply_filters(), ->check_delete_permission(), ->set_param(), wp_trash_post(), get_post(), ->prepare_item_for_response(), __() |
!is_wp_error() && ->check_delete_permission() | 69–73 | ->get_post(), is_wp_error(), apply_filters(), ->check_delete_permission(), ->set_param(), ->prepare_item_for_response(), wp_delete_post(), ->get_data(), deleted(), do_action() |
!is_wp_error() && ->check_delete_permission() | 69–73 | ->get_post(), is_wp_error(), apply_filters(), ->check_delete_permission(), ->set_param(), ->prepare_item_for_response(), wp_delete_post(), ->get_data(), deleted(), __() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 134 instructions, 12–73 executed per call, 8 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while WP_REST_Posts_Controller::delete_item() runs, in this order:
- apply_filters( rest_{$this->post_type}_trashable )filterline 1123 (+33 into the body)
Filters whether a post is trashable.
- do_action( rest_delete_{$this->post_type} )actionline 1200 (+110 into the body)
Fires immediately after a single post is deleted or trashed via the REST API.
Uses · 13
- is_wp_error()Checks whether the given variable is a WordPress Error.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- __()Retrieves the translation of $text.
- rest_authorization_required_code()Returns a contextual HTTP error code for authorization failure.
- wp_delete_post()Trashes or deletes a post or page.
- wp_trash_post()Moves a post or page to the Trash
- get_post()Retrieves post data given a post ID or post object.
- do_action()Calls the callback functions that have been added to an action hook.
- WP_REST_Posts_Controller::get_post()Gets the post, if the ID is valid.
- WP_REST_Posts_Controller::check_delete_permission()Checks if a post can be deleted.
- WP_Error::__construct()Initializes the error.
- WP_REST_Posts_Controller::prepare_item_for_response()Prepares a single post output for response.
Show all 13
- WP_REST_Response::__construct()
Used by · 2
- WP_REST_Font_Faces_Controller::delete_item()Deletes a single font face.
- WP_REST_Font_Families_Controller::delete_item()Deletes a single font family.
Source code
public function delete_item( $request ) { $post = $this->get_post( $request['id'] ); if ( is_wp_error( $post ) ) { return $post; } $id = $post->ID; $force = (bool) $request['force']; $supports_trash = ( EMPTY_TRASH_DAYS > 0 ); if ( 'attachment' === $post->post_type ) { $supports_trash = $supports_trash && MEDIA_TRASH; } /** * Filters whether a post is trashable. * * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug. * * Possible hook names include: * * - `rest_post_trashable` * - `rest_page_trashable` * - `rest_attachment_trashable` * * Pass false to disable Trash support for the post. * * @since 4.7.0 * * @param bool $supports_trash Whether the post type support trashing. * @param WP_Post $post The Post object being considered for trashing support. */ $supports_trash = apply_filters( "rest_{$this->post_type}_trashable", $supports_trash, $post ); if ( ! $this->check_delete_permission( $post ) ) { return new WP_Error( 'rest_user_cannot_delete_post', __( 'Sorry, you are not allowed to delete this post.' ), array( 'status' => rest_authorization_required_code() ) ); } $request->set_param( 'context', 'edit' ); // If we're forcing, then delete permanently. if ( $force ) { $previous = $this->prepare_item_for_response( $post, $request ); $result = wp_delete_post( $id, true ); $response = new WP_REST_Response(); $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data(), ) ); } else { // If we don't support trashing for this type, error out. if ( ! $supports_trash ) { return new WP_Error( 'rest_trash_not_supported', /* translators: %s: force=true */ sprintf( __( "The post does not support trashing. Set '%s' to delete." ), 'force=true' ), array( 'status' => 501 ) ); } // Otherwise, only trash if we haven't already. if ( 'trash' === $post->post_status ) { return new WP_Error( 'rest_already_trashed', __( 'The post has already been deleted.' ), array( 'status' => 410 ) ); } /* * (Note that internally this falls through to `wp_delete_post()` * if the Trash is disabled.) */Changelog
Introduced in 4.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.