Fixing Non-Contiguous cid and mid Values for Typecho Posts, Categories, and Tags!
Machine-translated (anthropic, claude-haiku-5-5) from the Chinese original. Read the original in 中文
Contents
Preface
Lately I've had a lot of classes, and there's really no material to write about. My life isn't worth recording either, and it really is boring, so I can only keep things going by reposting. The purpose of this article is to solve the problem of non-contiguous cid for posts and non-contiguous mid for categories and tags in Typecho. For some bloggers with obsessive-compulsive tendencies, non-contiguous mid and cid are unbearable to look at. As for me, I don't mind at all, haha~
Main Content
Save the following two code blocks as separate PHP files, upload them to the website root directory, and then visit them in your browser.
Special reminder: Please run this on a server with PHP 7.0 or below. This operation involves the database, so please make a backup in advance.
Solving the Non-Contiguous cid Problem
Rename the code to cid.php
<?php
/**
* Typecho重新排列不连续的文章ID
* 作者:http://blog.yuweiji.com/
*/
$hostname_blog = "localhost";
$database_blog = "数据库名";
$username_blog = "数据库用户名";
$password_blog = "数据库密码";
$blog = mysql_pconnect($hostname_blog, $username_blog, $password_blog) or trigger_error(mysql_error(),E_USER_ERROR);
$no = 1;
function change_id($cid)
{
global $no;
// 修改post cid,并修改分类、标签、自定义字段、评论的对应关系
$sql = 'update typecho_contents set cid = ' . $no . ' where cid = ' . $cid;
mysql_query($sql);
$sql = 'update typecho_relationships set cid = ' . $no . ' where cid = ' . $cid;
mysql_query($sql);
$sql = 'update typecho_comments set cid = ' . $no . ' where cid = ' . $cid;
mysql_query($sql);
$no = $no + 1;
}
mysql_select_db($database_blog, $blog);
$query_postRecord = "SELECT cid FROM typecho_contents ORDER BY cid ASC";
$all_postRecord = mysql_query($query_postRecord);
$row_postRecord = mysql_fetch_assoc($all_postRecord);
do {
change_id( $row_postRecord['cid'] );
} while ($row_postRecord = mysql_fetch_assoc($all_postRecord));
// 重新设置post id自增起点
mysql_query('alter table typecho_contents AUTO_INCREMENT = ' . $no);
echo 'ok';
?>Solving the Non-Contiguous mid Problem for Categories and Tags
Rename the file to mid.php
Prerequisite: After rearranging the mid values for categories and tags, the parent category of subcategories may be incorrect and needs to be modified manually. If there are no second-level categories, you can skip this.
<?php
/**
* Typecho重新排列分类和标签(meta)不连续的mid
* 作者:http://blog.yuweiji.com/
*/
$hostname_blog = "localhost";
$database_blog = "数据库名";
$username_blog = "数据库用户名";
$password_blog = "数据库密码";
$blog = mysql_pconnect($hostname_blog, $username_blog, $password_blog) or trigger_error(mysql_error(),E_USER_ERROR);
$no = 1;
function change_id($mid)
{
global $no;
// 修改meta id,并修改分类、标签、自定义字段、评论的对应关系
$sql = 'update typecho_metas set mid = ' . $no . ' where mid = ' . $mid;
mysql_query($sql);
$sql = 'update typecho_relationships set mid = ' . $no . ' where mid = ' . $mid;
mysql_query($sql);
$no = $no + 1;
}
mysql_select_db($database_blog, $blog);
$query_postRecord = "SELECT mid FROM typecho_metas ORDER BY mid ASC";
$all_postRecord = mysql_query($query_postRecord);
$row_postRecord = mysql_fetch_assoc($all_postRecord);
do {
change_id( $row_postRecord['mid'] );
} while ($row_postRecord = mysql_fetch_assoc($all_postRecord));
// 重新设置meta id自增起点
mysql_query('alter table typecho_metas AUTO_INCREMENT = ' . $no);
echo 'ok';
?>
Comments