Renaming an image leaves the old URL in comments and term descriptions, with "Unknown column 'ID'" database errors
维护者通常 1 天内回复
@girishpanchal30 已经在做这个了。
开始于 2026年10月29日。
评估
调研方向
从 inc/media_rename/attachment_db_renamer.php 开始:sql_handle_column()(约 L249-280)构建 WHERE ID,而 php_handle_column()(L364-375)已经基于 $primary_keys 构建了 where 子句。将 $primary_keys 传递下去,仅在 $wpdb->update() 成功时才递增 $count,并将跳过列表与带前缀的表名进行比较。完成的标准是:重命名一张图片时能改写 wp_comments.comment_content 和 wp_term_taxonomy.description,且 debug.log 中没有 Unknown column 'ID' 错误。
由索引模型根据 Issue 内容生成。
描述
Description
When you rename an image with "Rename attached file", Optimole searches the database for the old URL and rewrites it. For columns with no serialized data it uses sql_handle_column(), which always runs the update as WHERE ID = <value>. Of the core single-site tables, only wp_posts and wp_users have an ID column. On the others the UPDATE fails with Unknown column 'ID' in 'where clause' and the row keeps the old URL. The old file has already been moved, so those images break.
Tables this affects on a normal site:
wp_comments(comment_content,comment_author_url), keycomment_IDwp_term_taxonomy.description(category, tag and product category descriptions), keyterm_taxonomy_id.$skip_tablesliststerm_taxonomy, but it is compared with prefixed names, so it never matches.wp_termmeta,wp_commentmeta,wp_links, when none of that column's rows are serialized
On any real site, wp_postmeta, wp_options and wp_usermeta contain serialized rows (attachment metadata, roles, capabilities), so their value columns go to php_handle_column() instead and never reach this UPDATE.
replace() also counts these failed updates as replacements, because ++$count runs even when $wpdb->update() returns false.
Expected: every reference to the old file is rewritten, using each table's own primary key, and no database errors are logged.
Step-by-step reproduction instructions
- On a test site with Optimole 4.2.17 active (no Optimole account is needed for rename), add
define( 'WP_DEBUG', true );anddefine( 'WP_DEBUG_LOG', true );towp-config.php. - Import an image and get its URL:
ID=$(wp media import photo.jpg --porcelain); URL=$(wp eval "echo wp_get_original_image_url($ID);") - Add three references to it:
wp post create --post_status=publish --post_title=Control --post_content="<img src=\"$URL\">"wp term create category Gallery --description="<img src=\"$URL\">"wp comment create --comment_post_ID=1 --comment_content="<img src=\"$URL\">"
- In wp-admin, go to Media > Library (list view), hover the image and click "Replace or Rename". Type
renamedin "Rename attached file" and click Rename. - Check the three references:
wp post list --title=Control --field=post_contentshowsrenamed.jpg(correct).wp term get category gallery --by=slug --field=descriptionstill showsphoto.jpg.wp comment list --fields=comment_ID,comment_contentstill showsphoto.jpg.
wp-content/debug.loghas one error for each failed table, for example:
WordPress database error Unknown column 'ID' in 'where clause' for query UPDATE `wp_comments` SET `comment_content` = '<img src=\"http://example.test/wp-content/uploads/2026/10/renamed.jpg\">...' WHERE `ID` = '7'
I have not run steps 1 to 6 on a live site. I ran the 4.2.17 Optml_Attachment_Db_Renamer class against WordPress core table definitions on MySQL 9.6. A minimal $wpdb built the UPDATE the same way wpdb::update() does. Output:
replace() returned: 4
WordPress database error Unknown column 'ID' in 'where clause' for query UPDATE `wp_comments` ... WHERE `ID` = '7'
WordPress database error Unknown column 'ID' in 'where clause' for query UPDATE `wp_term_taxonomy` ... WHERE `ID` = '3'
WordPress database error Unknown column 'ID' in 'where clause' for query UPDATE `wp_termmeta` ... WHERE `ID` = '1'
wp_posts.post_content renamed
wp_comments.comment_content STILL OLD URL
wp_term_taxonomy.description STILL OLD URL
wp_termmeta.meta_value STILL OLD URL
Screenshots, screen recording, code snippet or Help Scout ticket
Columns with no serialized rows are sent to sql_handle_column(), and the table's primary keys are not passed in, https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/media_rename/attachment_db_renamer.php#L177-L183
The ID is the value of the first field whose name contains "id" (comment_ID, term_taxonomy_id, meta_id...), but the WHERE column is always ID, https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/media_rename/attachment_db_renamer.php#L249-L280
$id_field = $row->ID ?? $row->id ?? null;
// ...falls back to the first field containing "id"
$wpdb->update(
$table,
[ $column => $new_content ],
[ 'ID' => $id_field ]
);
++$count;
php_handle_column() already builds the WHERE clause from $primary_keys, https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/media_rename/attachment_db_renamer.php#L364-L375. Passing $primary_keys to sql_handle_column() and doing the same there would likely fix it. Only counting rows where update() succeeds would fix the count.
The skip list never matches because wpdb::tables() returns prefixed names, https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/media_rename/attachment_db_renamer.php#L20 and https://github.com/Codeinwp/optimole-wp/blob/v4.2.17/inc/media_rename/attachment_db_renamer.php#L67-L70 (core: https://github.com/WordPress/WordPress/blob/7.1.3/wp-includes/class-wpdb.php#L1159-L1166).
Core keys: comment_ID https://github.com/WordPress/WordPress/blob/7.1.3/wp-admin/includes/schema.php#L101-L117, term_taxonomy_id https://github.com/WordPress/WordPress/blob/7.1.3/wp-admin/includes/schema.php#L74-L81, meta_id https://github.com/WordPress/WordPress/blob/7.1.3/wp-admin/includes/schema.php#L56-L61. wpdb::update() writes the $where keys into the query as given, https://github.com/WordPress/WordPress/blob/7.1.3/wp-includes/class-wpdb.php#L2706-L2722, and logs the failure with error_log(), https://github.com/WordPress/WordPress/blob/7.1.3/wp-includes/class-wpdb.php#L1815-L1823.
Environment info
- Optimole 4.2.17. This code is the same on
mastertoday and has not changed since 4.0.0. - WordPress 6.2 to 7.1 (the core table keys are the same in all of them), any MySQL or MariaDB version
- PHP 7.4 and later
Is the issue you are reporting a regression
No. The same [ 'ID' => $id_field ] update was already in 4.0.0, when rename was added (#903).
Found while writing the WordPress compatibility checks for Optimole.
- 主要语言
- PHP
- 星标
- 74
- 派生
- 14
- 平均合并
- 1 天 21 小时
- 30 天内合并 PR
- 30
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
Codeinwp/optimole-wp 的其他 Issue
-
Page profiler endpoint fatals on PHP 8 when the request has no `b` (background selectors) field可能已有人在做 @girishpanchal30 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 88/100
Codeinwp/optimole-wp#1204 · 4 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Background image with a gradient is not lazy-loaded on WordPress 7.1 (Quote, Pullquote, Verse, Post Content, Accordion)可能已有人在做 @girishpanchal30 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 78/100
Codeinwp/optimole-wp#1203 · 3 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Page profiler never treats the detected LCP image as LCP, so it gets no fetchpriority="high" (stored ID is a string, compared with === to an int)可能已有人在做 @girishpanchal30 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 88/100
Codeinwp/optimole-wp#1202 · 3 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Account sign-up fatals when the Optimole API returns an empty or non-JSON response可能已有人在做 @girishpanchal30 今天认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 85/100
Codeinwp/optimole-wp#1200 · 3 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
bug-report customer report
难度 4/5 3-5 天 新手友好度 56/100
Codeinwp/optimole-wp#1211 ·
维护者通常 1 天内回复
查看 Codeinwp/optimole-wp 的全部 Issue
相似的 Issue
-
ServiceMapMissingAnalyser reads @method in docblock prose as a tag and reports a phantom accessor未关闭agent-ready area:static-analysis bug p3
难度 2/5 1-3 小时 新手友好度 82/100
gacela-project/gacela#1050 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
grokability/massdriver-server#38 ·
维护者通常 1 天内回复
-
bug review needed
难度 1/5 1 小时以内 新手友好度 86/100
OpenMage/magento-lts#5816 ·
维护者通常 2 天内回复
-
priority:low type:docs
难度 2/5 1-3 小时 新手友好度 84/100
crazy-goat/rabbit-stream#958 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
pterodactyl/panel#7310 ·
维护者通常 1 天内回复