On this page
wp_trash_post_comments( int|WP_Post|null $post = null ): mixed|void
Moves comments for a post to the Trash.
Parameters
$postint|WP_Post|null Optional-
Post ID or post object. Defaults to global $post.
Default:
null
Return
mixed|void False on failure.
Source
File: wp-includes/post.php. View all references
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;
}
Hooks
- do_action( 'trashed_post_comments',
int $post_id ,array $statuses ) -
Fires after comments are sent to the Trash.
- do_action( 'trash_post_comments',
int $post_id ) -
Fires before comments are sent to the Trash.
Related
Uses
| Uses | Description |
|---|---|
| add_post_meta() wp-includes/post.php | Adds a meta field to the given post. |
| wpdb::update() wp-includes/class-wpdb.php | Updates a row in the table. |
| clean_comment_cache() wp-includes/comment.php | Removes a comment from the object cache. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
| wpdb::get_results() wp-includes/class-wpdb.php | Retrieves an entire SQL result set from the database (i.e., many rows). |
| wpdb::prepare() wp-includes/class-wpdb.php | Prepares a SQL query for safe execution. |
Used By
| Used By | Description |
|---|---|
| WP_Customize_Manager::trash_changeset_post() wp-includes/class-wp-customize-manager.php | Trashes or deletes a changeset post. |
| wp_trash_post() wp-includes/post.php | Moves a post or page to the Trash |
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_trash_post_comments