wp_delete_post( int $post_id = 0, bool $force_delete = false ): WP_Post|false|null
- Since
- 1.0.0
- Source
wp-includes/post.php:3834
Description
When the post and page is permanently deleted, everything that is tied to it is deleted also. This includes comments, post meta fields, and terms associated with the post.
The post or page is moved to Trash instead of permanently deleted unless Trash is disabled, item is already in the Trash, or $force_delete is true.
Compatibility
- WordPress
- since 1.0.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
$post_idintoptional- Post ID. (The default of 0 is for historical reasons; providing it is incorrect.)Default:
0 $force_deletebooloptional- Whether to bypass Trash and force deletion.
Default false.Default:false
Return value
WP_Post|false|null- Post data on success, false or null on failure.
Performance profile
How much work a call to wp_delete_post() 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
- Scales with input
- Instructions
- 16–224
- Plugin surface
- 7 hooks
- Called by
- 29
Reaches the database via ->get_row().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 263.
Third-party callbacks on 'pre_delete_post', 'before_delete_post', 'delete_post_{$post->post_type}' run inside this call, and their cost is not bounded by anything here.
29 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - sqldatabase query
->get_row()called directly - cacheobject cache
wp_cache_delete()one call below wp_delete_post()
Further down the call graph this can also reach 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 · 19 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_delete_post() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$post_id | 16 | __(), _doing_it_wrong() |
!$post_id | 21 | ->prepare(), ->get_row() |
!$post_id | 33–39 | ->prepare(), ->get_row(), get_post(), wp_delete_attachment() |
!$post_id && $check !== null | 38–44 | ->prepare(), ->get_row(), get_post(), apply_filters() |
!$post_id | 39–42 | ->prepare(), ->get_row(), get_post(), get_post_status(), wp_trash_post() |
!$post_id | 41–46 | ->prepare(), ->get_row(), get_post(), get_post_status(), wp_delete_attachment() |
!$post_id && $check !== null | 46–51 | ->prepare(), ->get_row(), get_post(), get_post_status(), apply_filters() |
!$post_id && $check === null && !is_post_type_hierarchical() | 146–155 | ->prepare(), ->get_row(), get_post(), apply_filters(), do_action(), delete_post_meta(), delete_post_meta(), get_object_taxonomies(), wp_delete_object_term_relationships(), is_post_type_hierarchical(), ->prepare(), ->get_col(), ->update(), wp_defer_comment_counting(), ->prepare(), ->get_col(), wp_defer_comment_counting(), ->prepare(), ->get_col(), do_action(), do_action(), ID() |
!$post_id && $check === null && !is_post_type_hierarchical() | 154–162 | ->prepare(), ->get_row(), get_post(), get_post_status(), apply_filters(), do_action(), delete_post_meta(), delete_post_meta(), get_object_taxonomies(), wp_delete_object_term_relationships(), is_post_type_hierarchical(), ->prepare(), ->get_col(), ->update(), wp_defer_comment_counting(), ->prepare(), ->get_col(), wp_defer_comment_counting(), ->prepare(), ->get_col(), do_action(), do_action(), ID() |
!$post_id && $check === null && is_post_type_hierarchical() | 163–172 | ->prepare(), ->get_row(), get_post(), apply_filters(), do_action(), delete_post_meta(), delete_post_meta(), get_object_taxonomies(), wp_delete_object_term_relationships(), is_post_type_hierarchical(), ->prepare(), ->get_results(), ->prepare(), ->get_col(), ->update(), wp_defer_comment_counting(), ->prepare(), ->get_col(), wp_defer_comment_counting(), ->prepare(), ->get_col(), do_action(), do_action(), ID() |
!$post_id && $check === null && is_post_type_hierarchical() | 171–179 | ->prepare(), ->get_row(), get_post(), get_post_status(), apply_filters(), do_action(), delete_post_meta(), delete_post_meta(), get_object_taxonomies(), wp_delete_object_term_relationships(), is_post_type_hierarchical(), ->prepare(), ->get_results(), ->prepare(), ->get_col(), ->update(), wp_defer_comment_counting(), ->prepare(), ->get_col(), wp_defer_comment_counting(), ->prepare(), ->get_col(), do_action(), do_action(), ID() |
!$post_id && $check === null && is_post_type_hierarchical() | 173–182 | ->prepare(), ->get_row(), get_post(), apply_filters(), do_action(), delete_post_meta(), delete_post_meta(), get_object_taxonomies(), wp_delete_object_term_relationships(), is_post_type_hierarchical(), ->prepare(), ->get_results(), post_type(), ->prepare(), ->get_col(), ->update(), wp_defer_comment_counting(), ->prepare(), ->get_col(), wp_defer_comment_counting(), ->prepare(), ->get_col(), do_action(), do_action(), ID() |
7 further outcomes, up to 224 instructions
!$post_id && $check === null && !is_post_type_hierarchical() | 177–190 | ->prepare(), ->get_row(), get_post(), apply_filters(), do_action(), delete_post_meta(), delete_post_meta(), get_object_taxonomies(), wp_delete_object_term_relationships(), is_post_type_hierarchical(), ->prepare(), ->get_col(), ->update(), wp_defer_comment_counting(), ->prepare(), ->get_col(), wp_defer_comment_counting(), ->prepare(), ->get_col(), do_action(), do_action(), ID(), do_action(), do_action(), clean_post_cache(), is_post_type_hierarchical(), do_action() |
!$post_id && $check === null && is_post_type_hierarchical() | 181–189 | ->prepare(), ->get_row(), get_post(), get_post_status(), apply_filters(), do_action(), delete_post_meta(), delete_post_meta(), get_object_taxonomies(), wp_delete_object_term_relationships(), is_post_type_hierarchical(), ->prepare(), ->get_results(), post_type(), ->prepare(), ->get_col(), ->update(), wp_defer_comment_counting(), ->prepare(), ->get_col(), wp_defer_comment_counting(), ->prepare(), ->get_col(), do_action(), do_action(), ID() |
!$post_id && $check === null && !is_post_type_hierarchical() | 185–197 | ->prepare(), ->get_row(), get_post(), get_post_status(), apply_filters(), do_action(), delete_post_meta(), delete_post_meta(), get_object_taxonomies(), wp_delete_object_term_relationships(), is_post_type_hierarchical(), ->prepare(), ->get_col(), ->update(), wp_defer_comment_counting(), ->prepare(), ->get_col(), wp_defer_comment_counting(), ->prepare(), ->get_col(), do_action(), do_action(), ID(), do_action(), do_action(), clean_post_cache(), is_post_type_hierarchical(), do_action() |
!$post_id && $check === null && is_post_type_hierarchical() | 194–207 | ->prepare(), ->get_row(), get_post(), apply_filters(), do_action(), delete_post_meta(), delete_post_meta(), get_object_taxonomies(), wp_delete_object_term_relationships(), is_post_type_hierarchical(), ->prepare(), ->get_results(), ->prepare(), ->get_col(), ->update(), wp_defer_comment_counting(), ->prepare(), ->get_col(), wp_defer_comment_counting(), ->prepare(), ->get_col(), do_action(), do_action(), ID(), do_action(), do_action(), clean_post_cache(), is_post_type_hierarchical(), do_action() |
!$post_id && $check === null && is_post_type_hierarchical() | 202–214 | ->prepare(), ->get_row(), get_post(), get_post_status(), apply_filters(), do_action(), delete_post_meta(), delete_post_meta(), get_object_taxonomies(), wp_delete_object_term_relationships(), is_post_type_hierarchical(), ->prepare(), ->get_results(), ->prepare(), ->get_col(), ->update(), wp_defer_comment_counting(), ->prepare(), ->get_col(), wp_defer_comment_counting(), ->prepare(), ->get_col(), do_action(), do_action(), ID(), do_action(), do_action(), clean_post_cache(), is_post_type_hierarchical(), do_action() |
!$post_id && $check === null && is_post_type_hierarchical() | 204–217 | ->prepare(), ->get_row(), get_post(), apply_filters(), do_action(), delete_post_meta(), delete_post_meta(), get_object_taxonomies(), wp_delete_object_term_relationships(), is_post_type_hierarchical(), ->prepare(), ->get_results(), post_type(), ->prepare(), ->get_col(), ->update(), wp_defer_comment_counting(), ->prepare(), ->get_col(), wp_defer_comment_counting(), ->prepare(), ->get_col(), do_action(), do_action(), ID(), do_action(), do_action(), clean_post_cache(), is_post_type_hierarchical(), do_action() |
!$post_id && $check === null && is_post_type_hierarchical() | 212–224 | ->prepare(), ->get_row(), get_post(), get_post_status(), apply_filters(), do_action(), delete_post_meta(), delete_post_meta(), get_object_taxonomies(), wp_delete_object_term_relationships(), is_post_type_hierarchical(), ->prepare(), ->get_results(), post_type(), ->prepare(), ->get_col(), ->update(), wp_defer_comment_counting(), ->prepare(), ->get_col(), wp_defer_comment_counting(), ->prepare(), ->get_col(), do_action(), do_action(), ID(), do_action(), do_action(), clean_post_cache(), is_post_type_hierarchical(), do_action() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 263 instructions, 16–224 executed per call, 22 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 · 7
7 hooks fire while wp_delete_post() runs, in this order:
- apply_filters( pre_delete_post )filterline 3871 (+37 into the body)
Filters whether a post deletion should take place.
- do_action( before_delete_post )actionline 3887 (+53 into the body)
Fires before a post is deleted, at the start of wp_delete_post().
- do_action( delete_post_{$post->post_type} )actionline 3956 (+122 into the body)
Fires immediately before a post is deleted from the database.
- do_action( delete_post )actionline 3967 (+133 into the body)
Fires immediately before a post is deleted from the database.
- do_action( deleted_post_{$post->post_type} )actionline 3985 (+151 into the body)
Fires immediately after a post is deleted from the database.
- do_action( deleted_post )actionline 3996 (+162 into the body)
Fires immediately after a post is deleted from the database.
- do_action( after_delete_post )actionline 4019 (+185 into the body)
Fires after a post is deleted, at the conclusion of wp_delete_post().
Uses · 18
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- get_post()Retrieves post data given a post ID or post object.
- get_post_status()Retrieves the post status based on the post ID.
- wp_trash_post()Moves a post or page to the Trash
- wp_delete_attachment()Trashes or deletes an attachment.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- do_action()Calls the callback functions that have been added to an action hook.
- delete_post_meta()Deletes a post meta field for the given post ID.
- wp_delete_object_term_relationships()Unlinks the object from the taxonomy or taxonomies.
- get_object_taxonomies()Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.
- is_post_type_hierarchical()Determines whether the post type is hierarchical.
Show all 18
- wp_delete_post_revision()Deletes a revision.
- wp_defer_comment_counting()Determines whether to defer comment counting.
- wp_delete_comment()Trashes or deletes a comment.
- delete_metadata_by_mid()Deletes metadata by meta ID.
- clean_post_cache()Will clean the post in the cache.
- wp_clear_scheduled_hook()Unschedules all events attached to the hook with the specified arguments.
Used by · 29
- WP_Customize_Manager::handle_dismiss_autosave_or_lock_request()Deletes a given auto-draft changeset or the autosave revision for a given changeset or delete changeset lock.
- WP_Customize_Manager::save_changeset_post()Saves the post for the loaded changeset.
- WP_Customize_Manager::trash_changeset_post()Trashes or deletes a changeset post.
- WP_Customize_Nav_Menu_Item_Setting::update()Creates/updates the nav_menu_item post for this setting.
- WP_Privacy_Requests_Table::process_bulk_action()Process bulk actions.
- WP_REST_Menu_Items_Controller::delete_item()Deletes a single nav menu item.
- WP_REST_Posts_Controller::delete_item()Deletes a single post.
- WP_REST_Revisions_Controller::delete_item()Deletes a single revision.
- WP_REST_Templates_Controller::delete_item()Deletes a single template.
- WP_REST_Templates_Controller::update_item()Updates a single template.
- _wp_after_delete_font_family()Deletes child font faces when a font family is deleted.
- _wp_delete_customize_changeset_dependent_auto_drafts()Deletes auto-draft posts associated with the supplied changeset.
Show all 29
- _wp_delete_orphaned_draft_menu_items()Deletes orphaned draft menu items
- _wp_delete_post_menu_item()Callback for handling a menu item when its original object is deleted.
- _wp_delete_tax_menu_item()Serves as a callback for handling a menu item when its original object is deleted.
- wp_ajax_delete_page()Handles deleting a page via AJAX.
- wp_ajax_delete_post()Handles deleting a post via AJAX.
- wp_ajax_save_attachment()Handles updating attachment attributes via AJAX.
- wp_delete_auto_drafts()Deletes auto-drafts for new posts that are > 7 days old.
- wp_delete_nav_menu()Deletes a navigation menu.
- wp_delete_post_revision()Deletes a revision.
- wp_delete_user()Delete user and optionally reassign posts and links to another user.
- wp_nav_menu_update_menu_items()Saves nav menu items.
- wp_scheduled_delete()Permanently deletes comments or posts of any type that have held a status of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
- wp_trash_post()Moves a post or page to the Trash
- wp_xmlrpc_server::blogger_deletePost()Deletes a post.
- wp_xmlrpc_server::wp_deletePage()Deletes a page.
- wp_xmlrpc_server::wp_deletePost()Deletes a post for any registered post type.
- wpmu_delete_user()Deletes a user and all of their posts from the network.
Source code
function wp_delete_post( $post_id = 0, $force_delete = false ) { global $wpdb; $post_id = (int) $post_id; if ( $post_id <= 0 ) { _doing_it_wrong( __FUNCTION__, __( 'The post ID must be greater than 0.' ), '6.9.0' ); return false; } $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) ); if ( ! $post ) { return $post; } $post = get_post( $post ); if ( ! $force_delete && ( 'post' === $post->post_type || 'page' === $post->post_type ) && 'trash' !== get_post_status( $post_id ) && EMPTY_TRASH_DAYS ) { return wp_trash_post( $post_id ); } if ( 'attachment' === $post->post_type ) { return wp_delete_attachment( $post_id, $force_delete ); } /** * Filters whether a post deletion should take place. * * @since 4.4.0 * * @param WP_Post|false|null $check Whether to go forward with deletion. Anything other than null will short-circuit deletion. * @param WP_Post $post Post object. * @param bool $force_delete Whether to bypass the Trash. */ $check = apply_filters( 'pre_delete_post', null, $post, $force_delete ); if ( null !== $check ) { return $check; } /** * Fires before a post is deleted, at the start of wp_delete_post(). * * @since 3.2.0 * @since 5.5.0 Added the `$post` parameter. * * @see wp_delete_post() * * @param int $post_id Post ID. * @param WP_Post $post Post object. */ do_action( 'before_delete_post', $post_id, $post ); delete_post_meta( $post_id, '_wp_trash_meta_status' ); delete_post_meta( $post_id, '_wp_trash_meta_time' ); wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) ); $parent_data = array( 'post_parent' => $post->post_parent ); $parent_where = array( 'post_parent' => $post_id ); if ( is_post_type_hierarchical( $post->post_type ) ) { // Point children of this page to its parent, also clean the cache of affected children. $children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $post_id, $post->post_type ); $children = $wpdb->get_results( $children_query ); if ( $children ) { $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) ); } } // Do raw query. wp_get_post_revisions() is filtered. $revision_ids = $wpdb->get_col(Changelog
Introduced in 1.0.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/post.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.