wordpress文章实现连续ID发布文章

WordPress发布文章的时候,ID是随机的,很是蛋疼!很多小伙伴用的ID当伪静态,都不知道发布多少文章了

下面就给出一个解决办法,免费分享给大家!!

重点来了

function.php文件最后添加如下代码即可

function keep_id_continuous()
{
    global $wpdb;
    $lastID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' OR post_status = 'draft' OR post_status = 'private' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ORDER BY ID DESC LIMIT 1");
    $wpdb->query("DELETE FROM $wpdb->posts WHERE ( post_status = 'auto-draft' OR ( post_status = 'inherit' AND post_type = 'revision' ) ) AND ID > $lastID");
    $lastID++;
    $wpdb->query("ALTER TABLE $wpdb->posts AUTO_INCREMENT = $lastID");
}

好了 ,添加上这个代码后,会按照最近发布的ID递增上去!!

 

上面那玩意有些不够顶,直接来套组合拳

1.解决ID不连续

// ID不连续
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' );

2.禁用自动保存

// 禁用自动保存
add_action( 'wp_print_scripts', 'disable_autosave' ) ;
function disable_autosave(){
wp_deregister_script( 'autosave' ) ;
}