Multi-user blog woblog about the automatic recycling mechanism of draft auto-draft

Multi-user blog woblog is optimized and modified based on wordpress (hereinafter referred to as wp), and auto-draft in wp is a headache, especially in a multi-user blog system with a huge number of users, which can generate a lot of Garbage data, there are also some methods to address this problem on the Internet. In the end, woblog wrote a piece of code that automatically recycles drafts based on its own characteristics, which can not only avoid excessive garbage, but also ensure the continuity of data. The principle is to automatically Drafts and drafts are two types of content that are not published after a certain period of time, and the system will automatically recycle and distribute them to other users. Specifically modify /admin/includes/post.php, please note that this code is only suitable for use in woblog, and needs to be modified under WP, because woblog adds several fields on the basis of WP. First find the following code:

function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
    $post_title = '';
    if ( ! empty( $_REQUEST['post_title'] ) ) {
        $post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ) );
    }

    $post_content = '';
    if ( ! empty( $_REQUEST['content'] ) ) {
        $post_content = esc_html( wp_unslash( $_REQUEST['content'] ) );
    }

    $post_excerpt = '';
    if ( ! empty( $_REQUEST['excerpt'] ) ) {
        $post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] ) );
    }

    if ( $create_in_db ) {

Add our modified code after if ($create_in_db) {as follows:

global $wpdb, $current_user;
$post = $wpdb->get_row( "select * from wp_posts where ((post_status = 'auto-draft' AND post_date < '" . date('Y-m-d H:i:s', time()-3600*24*3) . "') OR (post_status = 'draft' AND post_date < '" . date('Y-m-d H:i:s', time()-3600*24*180) . "')) AND post_type = '$post_type' ORDER BY ID LIMIT 1" );
if( !$post ) {
    $post_id = wp_insert_post(
        array(
            'post_title'  => __( 'Auto Draft' ),
            'post_type'   => $post_type,
            'post_status' => 'auto-draft',
        )
    );
    $post    = get_post( $post_id );
} else {
    $post->post_date = date('Y-m-d H:i:s', time());
    $post->post_author = $current_user->ID;
    $post->blog_id = $wpdb->blogid;
    $post->post_title = '';
    $post->post_content = '';
    $post->post_name = '';
    $post->is_original = 0;
    $wpdb->query( "update wp_posts set post_title='',post_content='',post_name='',post_status = 'auto-draft',guid='',post_date='" . $post->post_date . "', post_author=" . $post->post_author . ", blog_id=" . $post->blog_id . ", is_original=0 where ID = " . $post->ID );
    $post->guid = get_permalink( $post->ID );
    $wpdb->query( "update wp_posts set guid='" . addslashes($post->guid) . "',post_date='" . $post->post_date . "', post_author=" . $post->post_author . ", blog_id=" . $post->blog_id . " where ID = " . $post->ID );
    $wpdb->query( "update wp_postmeta set meta_value='' where post_id = " . $post->ID );

}

The function of the above code is: if there is a record that can be recycled in the database, it will be recycled, if not, a new record will be created; the rules for recycling are: automatic drafts over 3 days or drafts over half a year. At the same time, it is best to add appropriate prompt sentences on the content list page to remind you to publish the draft content as soon as possible to prevent it from being recycled. 

This article is an original article, please indicate the source for reproduced!