WordPress 4.1.1 日志ID不连续的解决办法(关闭自动保存、自动草稿和禁用文章修订版)

Pop:这个是一个老问题了,这个方法可以使4.1.1最新版的WordPress的ID进行连续。

原理是获取最早一条自动草稿作为当前文章使用,如此可以实现在原有自动草稿功能基础上保持文章ID连续,并去除了删除7天前自动草稿的功能。

1、打开 wp-config.php 文件,在 $table_prefix = 'wp_'; 前面添加如下代码(注意,一定是 $table_prefix = 'wp_'; 这行的前面):

define('WP_POST_REVISIONS', false);
define('AUTOSAVE_INTERVAL', 86400);

2、找到并打开 wp-admin\post-new.php 这个文件,将其 wp_enqueue_script( 'autosave' ); 注释或删除掉

//wp_enqueue_script( 'autosave' );

3、找到并打开 wp-admin\post.php 这个文件,将其 wp_enqueue_script('autosave'); 注释或删除掉,并在前面一行添加 ;

if ( 'attachment' !== $post_type );
//wp_enqueue_script('autosave');

4、打开 wp-admin\includes\post.php 文件,找到

if ( $create_in_db ) {
$post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
$post = get_post( $post_id );
if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
set_post_format( $post, get_option( 'default_post_format' ) );
} else {

将其替换为:

global $wpdb; // 仅 wordpress 4.0 版本需要添加这句,否则报错 Call to a member function get_row() on a non-object
if ( $create_in_db ) {
global $current_user;//获取当前登录管理用户
$post = $wpdb->get_row( "SELECT * FROM $wpdb->posts WHERE post_status = 'auto-draft' AND post_type = '$post_type' AND post_author = $current_user->ID ORDER BY ID ASC 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 );
}
if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
set_post_format( $post, get_option( 'default_post_format' ) );
} else {

同时建议配合使用WP Cleaner

上面已经说明了方案的原理,请需要的童鞋结合代码注释看,方案的解决方法是,如果数据库已经有不可见的自动草稿日志,那么点击添加新文章时,将不会再自动产生新的不可见自动草稿,而是直接调用最早的一篇不可见草稿作为当前文章所需的数据库记录来使用,同时为避免ID不连续,删除了“删除早于7天的自动草稿”的代码语句!

Related Posts