stuttter / wp-user-activity

The best way to log activity in WordPress
https://wordpress.org/plugins/wp-user-activity/
GNU General Public License v2.0
42 stars 13 forks source link

Feature Request: Auto trash n activites after d number of days #25

Closed knutsp closed 8 months ago

knutsp commented 3 years ago

The biggest issue with this plugin is probably the lack of trashing, either after time or by maximum number reached.

Like this, in a cron every night:

$days_to_keep = apply _filters( 'wp_user_activity_trash_days_to_keep', 14 );
$max_trash_each_time = apply _filters( 'wp_user_activity_trash_each', 100 );
$activities = get_posts( [
    'post_type'      => 'activity',
    'date_query'     => [ 'before' => date( 'Y-m-d H:i:s', strtotime(  -$days_to_keep . ' days' ) ) ],
    'orderby'        => 'post_date',
    'order'          => 'DESC',
    'posts_per_page' => $max_trash_each_time,
] );

if ( is_array( $activities ) ) {
    foreach( $activities as $activity ) {
        wp_trash_post( $activity->ID );
    }
}

Direct WP_Query may be better than get_posts, since one may further filter the trashing query by meta.