On this page
wp_untrash_post_comments( int|WP_Post|null $post = null ): true|void
Restores comments for a post from the Trash.
Parameters
$postint|WP_Post|null Optional-
Post ID or post object. Defaults to global $post.
Default:
null
Return
true|void
Source
File: wp-includes/post.php. View all references
function wp_untrash_post_comments( $post = null ) {
global $wpdb;
$post = get_post( $post );
if ( ! $post ) {
return;
}
$post_id = $post->ID;
$statuses = get_post_meta( $post_id, '_wp_trash_meta_comments_status', true );
if ( ! $statuses ) {
return true;
}
/**
* Fires before comments are restored for a post from the Trash.
*
* @since 2.9.0
*
* @param int $post_id Post ID.
*/
do_action( 'untrash_post_comments', $post_id );
// Restore each comment to its original status.
$group_by_status = array();
foreach ( $statuses as $comment_id => $comment_status ) {
$group_by_status[ $comment_status ][] = $comment_id;
}
foreach ( $group_by_status as $status => $comments ) {
// Sanity check. This shouldn't happen.
if ( 'post-trashed' === $status ) {
$status = '0';
}
$comments_in = implode( ', ', array_map( 'intval', $comments ) );
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) );
}
clean_comment_cache( array_keys( $statuses ) );
delete_post_meta( $post_id, '_wp_trash_meta_comments_status' );
/**
* Fires after comments are restored for a post from the Trash.
*
* @since 2.9.0
*
* @param int $post_id Post ID.
*/
do_action( 'untrashed_post_comments', $post_id );
}
Hooks
- do_action( 'untrashed_post_comments',
int $post_id ) -
Fires after comments are restored for a post from the Trash.
- do_action( 'untrash_post_comments',
int $post_id ) -
Fires before comments are restored for a post from the Trash.
Related
Uses
| Uses | Description |
|---|---|
| delete_post_meta() wp-includes/post.php | Deletes a post meta field for the given post ID. |
| wpdb::query() wp-includes/class-wpdb.php | Performs a database query, using current database connection. |
| 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_meta() wp-includes/post.php | Retrieves a post meta field for the given post ID. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
| wpdb::prepare() wp-includes/class-wpdb.php | Prepares a SQL query for safe execution. |
Used By
| Used By | Description |
|---|---|
| wp_untrash_post() wp-includes/post.php | Restores a post from 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_untrash_post_comments