wppaste
WordPress

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
Trashes or deletes a post or page.

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

Reaches the database via ->get_row().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
16–224

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 263.

Plugin surface
7 hooks

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.

Called by
29

29 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • sqldatabase query->get_row()called directly
  • cacheobject cachewp_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.

WhenInstructionsCalls it makes
$post_id16__(), _doing_it_wrong()
!$post_id21->prepare(), ->get_row()
!$post_id33–39->prepare(), ->get_row(), get_post(), wp_delete_attachment()
!$post_id && $check !== null38–44->prepare(), ->get_row(), get_post(), apply_filters()
!$post_id39–42->prepare(), ->get_row(), get_post(), get_post_status(), wp_trash_post()
!$post_id41–46->prepare(), ->get_row(), get_post(), get_post_status(), wp_delete_attachment()
!$post_id && $check !== null46–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:

  1. apply_filters( pre_delete_post )filterline 3871 (+37 into the body)

    Filters whether a post deletion should take place.

  2. do_action( before_delete_post )actionline 3887 (+53 into the body)

    Fires before a post is deleted, at the start of wp_delete_post().

  3. do_action( delete_post_{$post->post_type} )actionline 3956 (+122 into the body)

    Fires immediately before a post is deleted from the database.

  4. do_action( delete_post )actionline 3967 (+133 into the body)

    Fires immediately before a post is deleted from the database.

  5. do_action( deleted_post_{$post->post_type} )actionline 3985 (+151 into the body)

    Fires immediately after a post is deleted from the database.

  6. do_action( deleted_post )actionline 3996 (+162 into the body)

    Fires immediately after a post is deleted from the database.

  7. 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

Show all 18

Used by · 29

Show all 29

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.