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

Pop的博客一直都是使用数字来定义文章ID的,每次对于新版的wordpress的自动保存草稿功能很是郁闷,它造成了大量的日志ID浪费,也使得日志ID不连续。

造成发布文章ID不连续的原因有三个:
一是自动保存功能 Auto-Save
二是历史版本功能 Post Revisions
三是自动草稿功能 Auto-Draft

针对WordPress 3.4.1,给出以下解决方法:

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

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

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

//wp_enqueue_script('autosave');

3、打开 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 {

将其替换为:

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 {

此方法是,如果数据库已经有不可见的自动草稿日志,那么点击添加新文章时,将不会再自动产生新的不可见自动草稿,而是直接调用最早的一篇不可见草稿作为当前文章所需的数据库记录来使用,同时为避免ID不连续,删除了“删除早于7天的自动草稿”的代码语句!

以上方法Pop转载于“冷色调”的博客,经过Pop测试,针对wordpress 3.4.1有效!

另根据自己亲测,在wp_posts表中,产生有自动草稿的ID,只要手动保持以下内容,那么,接着发表的文件会自动占用这个ID:

post_status
auto-draft

guid
http://youdomain/?p=id

post_type
post

喜欢研究的同学可以看下!

原文地址:
http://www.528628.com/wordpress-3-4-1-close-automatically-disable-articles-automatically-save-drafts-and-revisions.html

Related Posts