wp_trash_post_comments( int|WP_Post|null $post = null ): mixed|void
- Since
- 2.9.0
- Source
wp-includes/post.php:4192
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
mixed|void- False on failure.
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
- Scaling
- Scales with input
- Instructions
- 8–55
- Plugin surface
- 2 hooks
- Called by
- 2
Reaches the database via ->get_results().
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 62.
Third-party callbacks on 'trash_post_comments', 'trashed_post_comments' 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
- querycontent query
get_post()called directly - hookthird-party callbacks
do_action()called directly - sqldatabase query
->get_results()called directly - cacheobject cache
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8 | get_post() |
| always | 26 | get_post(), do_action(), ->prepare(), ->get_results() |
| always | 54–55 | get_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:
- do_action( trash_post_comments )actionline 4210 (+18 into the body)
Fires before comments are sent to the Trash.
- do_action( trashed_post_comments )actionline 4238 (+46 into the body)
Fires after comments are sent to the Trash.
Uses · 4
- 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.
- add_post_meta()Adds a meta field to the given post.
- clean_comment_cache()Removes a comment from the object cache.
Used by · 2
- WP_Customize_Manager::trash_changeset_post()Trashes or deletes a changeset post.
- wp_trash_post()Moves a post or page to the Trash
Source code
function wp_trash_post_comments( $post = null ) { global $wpdb; $post = get_post( $post ); if ( ! $post ) { return; } $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; } // 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.
Signature, return type and hooks compared across 5 parsed releases.
mixed|void to int|false|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.