WP 6.9 Tag Synchronization Script In WP 7.0 Complete Troubleshooting and Solution Records
1. Preface
Reference: Blog Label Translation Practice | 8060 tags, implement the whole process based on PHP scripts. Use a set of custom PHP scripts to traverse Chinese labels in batches, and automatically synchronize the missing English translation tags. The script runs stably in WordPress version 6.9, with zero errors and zero data exceptions, which can perfectly realize the 1:1 alignment of Chinese and English labels.
After the site is upgraded to WordPress 7.0 recently, the script is suddenly abnormal: no error is reported, it can run normally, but the missing English label cannot be filled, and there is a problem of inconsistency in the number of Chinese and English labels. After half-day all-round investigation, multiple code rewriting, database repair, and pit test, the core problem was finally positioned and solved perfectly. This article completes the complete investigation process, source code, operation log, failure cause and final solution.
2. Initial environment and basic configuration
– Site program: WordPress 7.0 (original stable version 6.9)
– Multilingual plugin: Polylang latest stable version
– Cache plugin: W3 Total Cache
– Running environment: Linux, PHP8.1, Alibaba Cloud RDS MySQL
– Script function: only read Chinese labels, automatically detect missing labels in English, only add English labels, do not modify or delete any original Chinese data
3. Fault phenomenon (after upgrading WP7.0)
After the site is upgraded to WP7.0, 3 Chinese labels (360, Sitemap, site map) are added, and the data of the Chinese and English labels in the background are as follows:
– Total number of Chinese labels: 8274 items
– Total number of English labels: 8271 items
– Data difference: missing 3 English translation tags
– Abnormal performance: the background English label is not synchronized
Execute the synchronization script that was originally available to WP6.9, run the log as follows, the script determines that there is no need to process the label, and it is completely invalid:
[root@iZ23wv7v5ggZ www.shuijingwanwq.com]# php polylang-batch-zh-to-en-tags.php
🚀 开始批量处理所有zh标签,自动添加en翻译(分页模式)...
📊 正在处理第 1 - 1000 个标签...
🔰 正在处理: 360 (ID: 36603)
📊 正在处理第 1001 - 2000 个标签...
📊 正在处理第 2001 - 3000 个标签...
📊 正在处理第 3001 - 4000 个标签...
📊 正在处理第 4001 - 5000 个标签...
🔰 正在处理: Sitemap (ID: 36607)
📊 正在处理第 5001 - 6000 个标签...
📊 正在处理第 6001 - 7000 个标签...
📊 正在处理第 7001 - 8000 个标签...
🔰 正在处理: 站点地图 (ID: 36604)
📊 正在处理第 8001 - 8274 个标签...
🎉 全部处理完成!
📊 统计:
已处理新标签: 0
已跳过已有翻译: 8271
✅ 所有标签都已经处理完毕,和手动添加的完全一样!
Core anomaly: The script detects that 8271 tags have been translated, and 0 are newly added, but the actual English labels are indeed missing 3, which is a false judgment that the translation failure has been translated.
4. Records of multiple rounds of error investigation and stepping on the pit
1. Misunderstanding 1: It is determined that the bottom API of WP7.0 is not compatible
It was initially suspected that WordPress 7.0 reconstructs terms, the underlying logic of the tag, and the old Polylang old API is invalidated. Try to modify the script: replace the native polylang translation binding method, use $wpdb to write the underlying write, manually add the language attribution field, and bind the translation group ID.
After the modification, a new fault occurs: the tag written directly by the database cannot be recognized by the polylang by the polylang, and all of it becomes the data without language.
2. Misunderstanding 2: The new dirty data cleaning logic has caused the script to be stuck and data is damaged
In order to solve the problem of duplicate dirty labels, a new batch query and delete SQL logic without language labels have been added. Since the total number of site labels exceeds 8,000, the heavyweight operation of the looped connection table query + deletion one by one directly causes the script to get stuck and have no output.
After forcibly terminated the script, catastrophic data abnormality occurred: the total number of English labels plummeted from 8000+ to 2 items (as shown in Figure 1), a large number of normal English tags were deleted by misjudgment, and the Chinese labels appeared redundantly added (as shown in Figure 2), and the data was completely confused.


3. Misunderstanding 3: Polylang version API parameters report an error
Try using the new Polylang official function pll_save_term_translations() Binding the translation relationship, running directly throws a php fatal error: the parameter type does not match, which confirms that there is a compatibility bug in the new version of the code adapted to AI, and cannot be universal.
5. Core root cause positioning (the final truth)
All code rewriting and underlying adaptation are invalid, and finally through the restore environment and clear cache test, the real failure cause is located:
After the WordPress 7.0 upgrade, the W3 Total Cache cache residual old Polylang translation relationship cache.
The cache residue causes the pll_get_term() function to read the cache data instead of the real database data. The three labels that are falsely judged are already in English, and the new logic is skipped directly, causing the script to fail.
This fault has nothing to do with the code version and the underlying API of WP, and is purely the data determination distortion caused by cache residues.
6. Final solution (zero modification of the original script, perfect repair)
1. Pre-repair operation
1. Restore the database to the clean backup before the WP7.0 upgrade through Alibaba Cloud RDS, and restore all disordered tag data;
2. Log in to the WordPress background → Performance (W3 Total Cache) → Dashboard → Clear all caches, and completely clear the remaining translation relationship cache and term cache;
3. Keep the original WP6.9 available script without any code modification.
2. The original script is finally available (WP6.9/7.0 is common)
polylang-batch-zh-to-en-tags.php
<?php
if (php_sapi_name() !== 'cli') {
die("❌ 请在命令行运行\n");
}
require __DIR__ . '/wp-config.php';
global $wpdb, $polylang;
// 配置:一次处理多少个,你可以根据自己的服务器调整
$per_page = 1000;
// 配置:源语言和目标语言,以后其他语言直接改这里就好
$source_lang = 'zh';
$target_lang = 'en';
echo "🚀 开始批量处理所有{$source_lang}标签,自动添加{$target_lang}翻译(分页模式)...\n\n";
$processed = 0;
$skipped = 0;
$offset = 0;
while (true) {
// 分页拿源语言标签
$terms = get_terms([
'taxonomy' => 'post_tag',
'lang' => $source_lang,
'hide_empty' => false,
'number' => $per_page,
'offset' => $offset,
]);
if (is_wp_error($terms) || empty($terms)) {
break;
}
$current_count = count($terms);
echo "📊 正在处理第 " . ($offset + 1) . " - " . ($offset + $current_count) . " 个标签...\n";
foreach ($terms as $term) {
$source_id = $term->term_id;
$name = $term->name;
$slug = $term->slug;
// 检查是不是已经有目标语言的翻译了
$target_id = pll_get_term($source_id, $target_lang);
if ($target_id) {
$skipped++;
continue;
}
echo "🔰 正在处理: {$name} (ID: {$source_id})\n";
// 创建目标语言标签,绕过slug重复检查
$wpdb->insert($wpdb->terms, [
'name' => $name,
'slug' => $slug,
'term_group' => 0
]);
$new_target_id = $wpdb->insert_id;
// 目标语言标签的taxonomy
$wpdb->insert($wpdb->term_taxonomy, [
'term_id' => $new_target_id,
'taxonomy' => 'post_tag',
'description' => '',
'parent' => 0,
'count' => 0
]);
// 设置目标语言
$polylang->model->term->set_language($new_target_id, $target_lang);
// 绑定翻译
$polylang->model->term->save_translations($source_id, [
$target_lang => $new_target_id
]);
$processed++;
}
$offset += $per_page;
unset($terms);
gc_collect_cycles();
}
echo "\n🎉 全部处理完成!\n";
echo "📊 统计:\n";
echo " 已处理新标签: {$processed}\n";
echo " 已跳过已有翻译: {$skipped}\n";
echo "\n✅ 所有标签都已经处理完毕,和手动添加的完全一样!\n";
3. The final successful run of the log
[root@iZ23wv7v5ggZ www.shuijingwanwq.com]# php polylang-batch-zh-to-en-tags.php
🚀 开始批量处理所有zh标签,自动添加en翻译(分页模式)...
📊 正在处理第 1 - 1000 个标签...
🔰 正在处理: 360 (ID: 36603)
📊 正在处理第 1001 - 2000 个标签...
📊 正在处理第 2001 - 3000 个标签...
📊 正在处理第 3001 - 4000 个标签...
📊 正在处理第 4001 - 5000 个标签...
🔰 正在处理: Sitemap (ID: 36607)
📊 正在处理第 5001 - 6000 个标签...
📊 正在处理第 6001 - 7000 个标签...
📊 正在处理第 7001 - 8000 个标签...
🔰 正在处理: 站点地图 (ID: 36604)
📊 正在处理第 8001 - 8274 个标签...
🎉 全部处理完成!
📊 统计:
已处理新标签: 3
已跳过已有翻译: 8271
✅ 所有标签都已经处理完毕,和手动添加的完全一样!
7. Final repair results
– Total number of Chinese labels: 8274 items (no increase or decrease, the original data is complete)
– Total number of English labels: 8274 items (3 missing tags are successfully filled)
– No-no-language dirty labels, no duplicate labels
– Chinese-English label 1:1 perfect alignment, translation relationship is bound normally
8. Summarize and avoid pit experience
1. After the WP version is upgraded, it is priority to clear the whole site cache and plug-in cache, and then test the original custom script. Do not blindly determine that the code is incompatible;
2. Polylang multi-language site, translation relationship and term judgment are highly dependent caches, and cache residues will lead to abnormal function false judgments;
3. The original script runs stably, and do not blindly rewrite the new version. Most of the ‘compatibility failures’ are caused by cache;
4. It is forbidden to add a full library to deletion logic in batch scripts, and it is easy to get stuck and delete normal data by mistake.