Remind Authors About Old Drafts
Once a week, emails each author a list of their drafts that have not been touched for a month, with edit links, so good ideas do not get forgotten.
Opens WPIDE on
Code
$wpide_stale_days = 30;
add_action( 'init', function () {
if ( ! wp_next_scheduled( 'wpide_stale_drafts' ) ) {
wp_schedule_event( time() + DAY_IN_SECONDS, 'weekly', 'wpide_stale_drafts' );
}
} );
add_action( 'wpide_stale_drafts', function () use ( $wpide_stale_days ) {
$drafts = get_posts( array(
'post_type' => 'post',
'post_status' => 'draft',
'numberposts' => 300,
'no_found_rows' => true,
'date_query' => array( array( 'before' => gmdate( 'Y-m-d H:i:s', time() - $wpide_stale_days * DAY_IN_SECONDS ), 'column' => 'post_modified_gmt' ) ),
) );
$by_author = array();
foreach ( $drafts as $draft ) {
$by_author[ (int) $draft->post_author ][] = $draft;
}
foreach ( $by_author as $author_id => $posts ) {
$user = get_userdata( $author_id );
if ( ! $user || ! is_email( $user->user_email ) ) {
continue;
}
$lines = array( sprintf( 'Hi %s, these drafts have not been edited for over %d days:', $user->display_name, $wpide_stale_days ), '' );
foreach ( array_slice( $posts, 0, 20 ) as $post ) {
$lines[] = sprintf( '• %s – %s', $post->post_title ? $post->post_title : '(no title)', admin_url( 'post.php?post=' . $post->ID . '&action=edit' ) );
}
$lines[] = '';
$lines[] = 'Finish them, schedule them or move them to the trash.';
wp_mail( $user->user_email, 'Your drafts are waiting', implode( "\n", $lines ) );
}
} );
Notes
Change the number of days at the top; each author gets at most one email per week listing up to 20 drafts.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.