wppaste
WordPress

wp_trash_post_comments( int|WP_Post|null $post = null ): int|false|null

Since
2.9.0
Source
wp-includes/post.php:4268
Moves comments for a post to the Trash.

Compatibility

WordPress
since 2.9.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

$postint|WP_Post|nulloptional
Post ID or post object. Defaults to global $post.Default: null

Return value

int|false|null
int if the comments were successfully deleted, false on failure and null if the post or comment did not exist.

Performance profile

How much work a call to wp_trash_post_comments() 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_results().

Scaling
Scales with input

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

Instructions
8–55

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

Plugin surface
2 hooks

Third-party callbacks on 'trash_post_comments', 'trashed_post_comments' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 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 callbacksdo_action()called directly
  • sqldatabase query->get_results()called directly
  • cacheobject cachewp_cache_delete_multiple()one call below wp_trash_post_comments()

Further down the call graph this can also reach serialize, option and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_trash_post_comments() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8get_post()
always26get_post(), do_action(), ->prepare(), ->get_results()
always54–55get_post(), do_action(), ->prepare(), ->get_results(), add_post_meta(), comment_post_ID(), array_keys(), clean_comment_cache(), 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: 62 instructions, 8–55 executed per call, 4 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_trash_post_comments() runs, in this order:

  1. do_action( trash_post_comments )actionline 4286 (+18 into the body)

    Fires before comments are sent to the Trash.

  2. do_action( trashed_post_comments )actionline 4314 (+46 into the body)

    Fires after comments are sent to the Trash.

Uses · 4

Used by · 2

Source code

function wp_trash_post_comments( $post = null ) {	global $wpdb; 	$post = get_post( $post ); 	if ( ! $post ) {		return null;	} 	$post_id = $post->ID; 	/**	 * Fires before comments are sent to the Trash.	 *	 * @since 2.9.0	 *	 * @param int $post_id Post ID.	 */	do_action( 'trash_post_comments', $post_id ); 	$comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) ); 	if ( ! $comments ) {		return null;	} 	// Cache current status for each comment.	$statuses = array();	foreach ( $comments as $comment ) {		$statuses[ $comment->comment_ID ] = $comment->comment_approved;	}	add_post_meta( $post_id, '_wp_trash_meta_comments_status', $statuses ); 	// Set status for all comments to post-trashed.	$result = $wpdb->update( $wpdb->comments, array( 'comment_approved' => 'post-trashed' ), array( 'comment_post_ID' => $post_id ) ); 	clean_comment_cache( array_keys( $statuses ) ); 	/**	 * Fires after comments are sent to the Trash.	 *	 * @since 2.9.0	 *	 * @param int   $post_id  Post ID.	 * @param array $statuses Array of comment statuses.	 */	do_action( 'trashed_post_comments', $post_id, $statuses ); 	return $result;}

Changelog

Introduced in 2.9.0. One change between 6.7.7 and 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.

7.1.0
Return type changed from mixed|void to int|false|null.verified against source
2.9.0
Introduced.from the docblock

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.