处理wordpress post_id不连续的方法

wp-config.php中添加:

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

wp-admin/post-new.php中注释掉:

wp_enqueue_script('autosave');

在数据库中删除修订版本:

DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type = ‘revision’;

删除一些冗余数据:

DELETE FROM wp_postmeta WHERE meta_key = ‘_edit_lock’;
DELETE FROM wp_postmeta WHERE meta_key = ‘_edit_last’;

在使用的主题目录下的function.php中添加:

function keep_id_continuous(){
global $wpdb;

// 删掉自动草稿和修订版
$wpdb->query(“DELETE FROM `$wpdb->posts` WHERE `post_status` = ‘auto-draft’ OR `post_type` = ‘revision'”);

// 自增值小于现有最大ID,MySQL会自动设置正确的自增值
$wpdb->query(“ALTER TABLE `$wpdb->posts` AUTO_INCREMENT = 1”);
}

add_filter( ‘load-post-new.php’, ‘keep_id_continuous’ );
add_filter( ‘load-media-new.php’, ‘keep_id_continuous’ );
add_filter( ‘load-nav-menus.php’, ‘keep_id_continuous’ );

发表评论