• The issue of Postie Plugin creating tmptitle draft posts ad infinitum when a problem is incurred with a media file or there’s not enough memory is a common one.

    The problem is two fold:

    1. Postie does not inform the website administrator that it is happening and sometimes it’s discovered days later when there are hundreds of draft posts.
    2. The problem causes a backlog of emails because Postie does not progress to the next mail when a problem is incurred.

    For that reason, I’ve created this function which appears to work fine. It simply emails the website administrator if two or more draft posts with the title ‘tmptitle’ are created.

    It would be great to see Postie Plugin do something like this out of the box. Even better if Postie were able to skip problem emails to avoid mail backlogs.

    Any suggested improvements are welcome!

    
    function check_draft_post_title( $new_status, $old_status, $post ) {
        if ( $new_status === 'draft' && strtolower( $post->post_title ) === 'tmptitle' ) {
            $post_id = $post->ID;
    
            // Check if transient already exists
            $email_sent = get_transient( 'draft_post_email_sent_' . $post_id );
    
            if ( false === $email_sent ) {
                $args = array(
                    'post_type'   => 'post',
                    'post_status' => 'draft',
                    'post_title' => 'tmptitle',
                );
                $query = new WP_Query( $args );
    
                // Check if there are 2 or more draft posts with the same title
                if ( $query->found_posts >= 2 ) {
                    error_log( 'Two or more draft posts with the title "tmptitle" have been created by Postie Plugin.' );
                    $to      = get_option( 'admin_email' ); // Use admin email address
    				$subject = get_bloginfo( 'name' ) . ' - Multiple Draft tmptitle Posts Created';
    				$message = '<p>Two or more draft posts with the title "tmptitle" have been created by Postie Plugin.</p>';
    				$message .= '<p>View all posts in the admin area: ' . get_admin_url() . 'edit.php?post_type=post</p>';
    				$message .= '<p>Here are some suggested remedies: https://postieplugin.com/ufaq/my-posts-have-a-tmptitle-title-and-no-content/</p>';
                    $headers = array( 'Content-Type: text/html; charset=UTF-8' );
                    wp_mail( $to, $subject, $message, $headers );
    
                    // Set transient to prevent duplicate email
                    set_transient( 'draft_post_email_sent_' . $post_id, true, DAY_IN_SECONDS );
                }
    
                wp_reset_postdata();
            }
        }
    }
    add_action( 'transition_post_status', 'check_draft_post_title', 10, 3 );
Viewing 1 replies (of 1 total)
  • Thread Starter ChrisL

    (@chrslcy)

    The original code seems to trigger on the first tmptitle post, however, if you increase the trigger value to 3, that seems to fix the issue, like this:

    function check_draft_post_title( $new_status, $old_status, $post ) {
        if ( $new_status === 'draft' && strtolower( $post->post_title ) === 'tmptitle' ) {
            $post_id = $post->ID;
    
            // Check if transient already exists
            $email_sent = get_transient( 'draft_post_email_sent_' . $post_id );
    
            if ( false === $email_sent ) {
                $args = array(
                    'post_type'   => 'post',
                    'post_status' => 'draft',
                    'post_title' => 'tmptitle',
                );
                $query = new WP_Query( $args );
    
                // Check if there are three or more draft posts with the title tmptitle
                if ( $query->found_posts >= 3 ) {
                    error_log( 'Three or more draft posts with the title "tmptitle" have been created by Postie Plugin.' );
                    $to      = get_option( 'admin_email' ); // Use admin email address
    		$subject = get_bloginfo( 'name' ) . ' - Multiple Draft tmptitle Posts Created';
    		$message = '<p>Three or more draft posts with the title "tmptitle" have been created by Postie Plugin.</p>';
    		$message .= '<p>View all posts in the admin area: ' . get_admin_url() . 'edit.php?post_type=post</p>';
    		$message .= '<p>Here are some suggested remedies: https://postieplugin.com/ufaq/my-posts-have-a-tmptitle-title-and-no-content/</p>';
                    $headers = array( 'Content-Type: text/html; charset=UTF-8' );
                    wp_mail( $to, $subject, $message, $headers );
    
                    // Set transient to prevent duplicate email
                    set_transient( 'draft_post_email_sent_' . $post_id, true, DAY_IN_SECONDS );
                }
    
                wp_reset_postdata();
            }
        }
    }
    add_action( 'transition_post_status', 'check_draft_post_title', 10, 3 );
Viewing 1 replies (of 1 total)
  • The topic ‘tmptitle draft posts’ is closed to new replies.