请选择 进入手机版 | 继续访问电脑版

 找回密码
 立即注册
查看: 1248|回复: 0

清空无用的wordpress id数字并且重新排序的代码

[复制链接]

6771

主题

8

回帖

2万

积分

管理员

积分
21718
发表于 2021-5-11 15:34:27 | 显示全部楼层 |阅读模式
众所周知,wordpress这款强大的免费系统,不论是已经发布的文章,还是上传的图片,还是缓存的文章,都占用1个ID数字号,这就导致了没有几篇文章,却有很多ID数字号的问题。用下面的这段代码,可以大幅度的清理掉没有用的ID数字号并给文章重新排序生成新的ID号。

  1. <?php
  2. /** 引入网站配置文件,这里主要获得数据库连接信息及常规操作类 */
  3. require_once './wp-config.php';
  4. function change_post_id($id)
  5. {
  6.         global $convertedrows, $wpdb;
  7.         /** 修改文章ID关联的类别、标签、、评论各表,prefix是您安装时设置的数据库表前缀 */
  8.         $wpdb->query('update ' . $wpdb->prefix . 'posts set ID = ' . $convertedrows . ' where ID = ' . $id);
  9.         $wpdb->query('update ' . $wpdb->prefix . 'term_relationships set object_id = ' . $convertedrows . ' where object_id = ' . $id);
  10.         $wpdb->query('update ' . $wpdb->prefix . 'postmeta set post_id = ' . $convertedrows . ' where post_id = ' . $id);
  11.         $wpdb->query('update ' . $wpdb->prefix . 'comments set comment_post_ID = ' . $convertedrows . ' where comment_post_ID = ' . $id);
  12.         $convertedrows++;
  13. }
  14. /** ID默认由1开始 */
  15. $convertedrows = 1;
  16. /** 库文章表所有记录 */
  17. $sql_query = 'SELECT ID FROM ' . $table_prefix . 'posts ORDER BY ID ASC';
  18. $all_post_ids = $wpdb->get_results($sql_query);
  19. /** 有返回值时则执行循环 */
  20. if (is_array($all_post_ids)) {
  21.         foreach ($all_post_ids as $post_id) {
  22.                 change_post_id($post_id->ID);
  23.         }
  24. }
  25. /** 重新设置文章ID自动增加的起点 */
  26. $wpdb->query('alter table ' . $table_prefix . 'posts AUTO_INCREMENT = ' . $convertedrows);
  27. echo 'Total:' . $convertedrows . ', It\'s ok! ';
  28. ?>
复制代码


保存的wordpress安装目录下面,运行即可。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表