Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.