I know how to programmatically delete comments that are X days old but they then go to the trash. How do I empty the comment trash every X days?
function md_scheduled_delete_commentsmd_scheduled_delete_comments() {
$comments = get_comments( array(
'post_type' => 'post',
'date_query' => array(
'before' => '3 days ago',
),
) );
if ( $comments ) {
foreach ( $comments as $comment ) {
wp_delete_comment( $comment );
}
}
}
add_action( 'wp_scheduled_delete', 'md_scheduled_delete_comments' );
// You can test it immediately by adding following line:
// add_action( 'wp_loaded', 'md_scheduled_delete_comments' );
I know how to programmatically delete comments that are X days old but they then go to the trash. How do I empty the comment trash every X days?
function md_scheduled_delete_commentsmd_scheduled_delete_comments() {
$comments = get_comments( array(
'post_type' => 'post',
'date_query' => array(
'before' => '3 days ago',
),
) );
if ( $comments ) {
foreach ( $comments as $comment ) {
wp_delete_comment( $comment );
}
}
}
add_action( 'wp_scheduled_delete', 'md_scheduled_delete_comments' );
// You can test it immediately by adding following line:
// add_action( 'wp_loaded', 'md_scheduled_delete_comments' );
Share
Improve this question
edited Aug 24, 2019 at 7:44
birgire
68.1k7 gold badges120 silver badges252 bronze badges
asked Aug 24, 2019 at 4:44
PetePete
1,0582 gold badges14 silver badges40 bronze badges
1 Answer
Reset to default 1When deleting a comment it's possible to use the second input argument:
wp_delete_comment( $comment, $force_delete = true )
to delete it permanently and avoiding the trash bin. You could try to adjust your scheduled script accordingly.
There's also the EMPTY_TRASH_DAYS
constant, that's default set to empty the trash bin after 30 days, but it will affect comments, posts, pages and other post types.