最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

automation - How to auto empty comment trash after X days

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

When 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.

发布评论

评论列表(0)

  1. 暂无评论