wordpress blog id不连续的解决

wp两个特性:自动保存(auto save) 和 版本记录(revisions)。

post_status: auto-draft, draft

auto-draft是系统自动生成,控制台不可见,draft是草稿。

post_type: post, revision, page

post_parent: 描述从哪个id演变而来。revision版的不为0,原始的为0.

修改配置文件

修改wp-config.php,增加两行:

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

第一行中单位是秒。 这里每10小时自动保存一次。

第二行是禁用版本历史的 Revisions的设置选项说明如下:

There are options, but they are hidden well :) There is a constant (not even filterable) that can be set by a plugin or in your wp-settings.php file:

WP_POST_REVISIONS:

true (default), -1: store every revision false, 0: do not store any revisions (except the one autosave per post) (int) > 0: store that many revisions (+1 autosave) per post. Old revisions are automatically deleted.

设置这个变量为false或0的话,还是会保留一个历史版本的。

另外,在每次新建文章时,会自动保存一个记录,要禁用的话,只能硬编码。

修改wp-admin/post.php和wp-admin/post-new.php,找到如下内容,注释掉。

wp_enqueue_script('autosave');

修改wp-admin/post-new.php,在代码的最后几行,有:

38 // Show post form.
39 $post = get_default_post_to_edit( $post_type, true);
40 $post_ID = $post->ID;
41 include('edit-form-advanced.php');
42 include('./admin-footer.php');
43 ?>

将第39行最后的true改为false:

39 $post = get_default_post_to_edit( $post_type, false);

硬编码修改

此外,还要修改wp-admin/include/post.php,找到如下代码:

418     if ( $create_in_db ) {
419 // Cleanup old auto-drafts more than 7 days old
420 $old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" );
421 foreach ( (array) $old_posts as $delete )
422 wp_delete_post( $delete, true ); // Force delete
423 $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
424 $post = get_post( $post_id );
425 if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
426 set_post_format( $post, get_option( 'default_post_format' ) );
427 } else {

将if分支中的内容,直到424行,修改成下面的内容:

418     if ( $create_in_db ) {
419 // modify by akii start
420 global $current_user;
421 $post_auto_draft = $wpdb->get_row( "SELECT * FROM $wpdb->posts WHERE post_status = 'auto-draft' AND post_author = $current_user->ID ORDER BY ID ASC LIMIT 1"|| );
422 if ($post_auto_draft){
423 $post = $post_auto_draft;
424 } else {
425 $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
426 $post = get_post( $post_id );
427 }
428 // modify by akii end
429 if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) )
430 set_post_format( $post, get_option( 'default_post_format' ) );
431 } else {

参考自 Akii Snow 的博客

禁用版本记录

修改wp-config.php,增加一行:

define('WP_POST_REVISIONS', false );

清理数据库中版本历史

  1. 手工清理
  2. 安装wp插件

自动保存的记录是如下记录:

select * from `wp_posts` where `post_status` = 'auto-draft' ;

而版本记录是:

select * from `wp_posts` where `post_status` = 'inherit';

手工保存的是:

select * from `wp_posts` where `post_status` = 'draft' ;

删除历史冗余记录

删除草稿记录,自动保存记录,版本变迁记录:

delete from `wp_posts` where `post_status` = 'inherit' or `post_status` = 'auto-draft' or `post_status` = 'draft';

禁用自动保存的插件

有老外写了这个禁用自动保存的插件, 文章在此

插件下载地址

删除版本历史,禁用版本保存的插件

disable revisions

将id顺序排列

涉及到4张表,都要更新。尝试使用循环解决,未遂。只得手工改。

下面的例子将id为108的记录修改id为7。

update `wp_posts` set `id` = 7 where `id` = 108;
update `wp_term_relationships` set `object_id` = 7 where `object_id` = 108;
update `wp_postmeta` set `post_id` = 7 where `post_id` = 108;
update `wp_comments` set `comment_post_ID` = 7 where `comment_post_ID` = 108;

新建博客仍有问题。新博客的id并不小!

保持id连续的原则:

  1. 不要删博客
  2. 新建的博客一定要保存
  3. 禁用自动保存和版本记录。

参考自: 月光博客

tagged by
comments powered by Disqus