如何在X天后自动清空评论垃圾桶

时间:2019-08-24 作者:Pete

我知道如何以编程方式删除X天以前的注释,但它们随后会被丢弃。如何每隔X天清空评论垃圾?

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\' );

1 个回复
最合适的回答,由SO网友:birgire 整理而成

删除评论时,可以使用second input argument:

wp_delete_comment( $comment, $force_delete = true ) 
要永久删除它并避免垃圾箱。您可以尝试相应地调整计划的脚本。

还有EMPTY_TRASH_DAYS 常量,默认设置为30天后清空垃圾箱,但它会影响评论、帖子、页面和其他帖子类型。