Skip to content

Renaming an image leaves the old URL in comments and term descriptions, with "Unknown column 'ID'" database errors #1199

Closed

Description

@kushh23

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), key comment_ID
  • wp_term_taxonomy.description (category, tag and product category descriptions), key term_taxonomy_id. $skip_tables lists term_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

  1. On a test site with Optimole 4.2.17 active (no Optimole account is needed for rename), add define( 'WP_DEBUG', true ); and define( 'WP_DEBUG_LOG', true ); to wp-config.php.
  2. Import an image and get its URL:
    ID=$(wp media import photo.jpg --porcelain); URL=$(wp eval "echo wp_get_original_image_url($ID);")
  3. 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\">"
  4. In wp-admin, go to Media > Library (list view), hover the image and click "Replace or Rename". Type renamed in "Rename attached file" and click Rename.
  5. Check the three references:
    • wp post list --title=Control --field=post_content shows renamed.jpg (correct).
    • wp term get category gallery --by=slug --field=description still shows photo.jpg.
    • wp comment list --fields=comment_ID,comment_content still shows photo.jpg.
  6. wp-content/debug.log has 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 master today 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.

Activity

  1. added
    bugThis label could be used to identify issues that are caused by a defect in the product.
    on Oct 28, 2026
  2. added theissue type on Oct 28, 2026
  3. girishpanchal30 commented on Oct 29, 2026

    @girishpanchal30
    Contributor

    I have fixed it with PR #1209

  4. vytisbulkevicius commented on Oct 29, 2026

    @vytisbulkevicius
    Contributor

    🧪 QA steps and test links

    After you rename an image with Optimole's "Rename attached file", references to it in comments and in category or tag descriptions keep the old file name. The old file is gone, so those images break, and the database logs an error for each table it failed to update. References in post content are updated correctly.

    Link Build Bot check
    Reproduce the bug Optimole 4.2.17 (released) ✅ Reproduced: after the rename, the comment's image was broken, the Gallery description still pointed at photo.jpg, and the log showed a database error for the UPDATE wp_comments query
    Verify the fix PR #1209 build 5b22686 ✅ Fixed: the comment's image loaded, the description pointed at renamed.jpg, and the log had no database errors

    In the links:

    • Steps 1 to 5 are already done, and the links open on the image's Edit Media screen.
    • Playground runs on SQLite, so its log says no such column: ID instead of MySQL's Unknown column 'ID'.
    • Tools → QA Logs shows debug.log.

    Reproduce on Optimole 4.2.17 (with WP_DEBUG_LOG on):

    1. Media → Add Media File: upload a JPG named photo.jpg, then copy its File URL.
    2. Posts → Add Post: title "Control", add the image, publish.
    3. On the published post, as admin, leave the comment <img src="<File URL>">.
    4. Posts → Categories: add "Gallery" with the description <a href="<File URL>">photo</a>.
    5. Media → Library (list view): hover the image and click Replace or Rename.
    6. In Rename attached file, type renamed and click Rename.
    7. Check Comments, Posts → Categories → Gallery, and the Control post.
    • Bug: the Control post shows the image, but the comment's image is broken and the Gallery description still links photo.jpg. debug.log has WordPress database error Unknown column 'ID' in 'where clause' for wp_comments and wp_term_taxonomy.
    • Fixed: all three show renamed.jpg, and no database errors are logged.
    Edge cases
    • Image linked in a tag description and a WooCommerce product category description: both follow the rename
    • Post content using a resized copy (photo-300x225.jpg): it follows the rename
    • Image over 2560px (WordPress makes a -scaled copy) used in a post and a comment: both follow the rename
    • Image used in a widget or Customizer setting: it follows the rename
    • Replace file on the same screen: still works as before

    Fix: #1209 (open)

  5. pirate-bot commented on Oct 29, 2026

    @pirate-bot
    Collaborator

    QA agent — 🟢 Passing checks · Image renaming updates the affected references without database errors.

    I reproduced stale references on the base build, then successfully repeated the same rename on the candidate. Comments, category and tag descriptions, metadata and links followed the new filename, including 105-row serialized data and resized or scaled URLs. File replacement also worked, replacement counts excluded failed writes, and normal renames logged no database errors.

    Checks (5 passed)
    • ✅ Renaming an image through Media Library rewrites plain-text references in posts, comments, term descriptions, term metadata, comment metadata, links and user URLs to the renamed file using the correct row keys, including resized and scaled image URLs with the site's port.
    • ✅ The rename rewrites all matching serialized references, including more than 100 rows, nested data, escaped URLs, resized and scaled image URLs, and escaped non-image URLs, while preserving valid data and unrelated values.
    • ✅ A normal image rename completes without database errors in the WordPress debug log.
    • ✅ The database renamer counts only successfully updated rows, counts duplicate URL matches in a row once, and returns zero for unchanged or failed updates.
    • ✅ Replacing an image file from the same attachment screen still updates the delivered image and any affected resized references without corrupting serialized data.

    Full report, evidence and session trail

  6. pirate-bot commented on Nov 2, 2026

    @pirate-bot
    Collaborator

    🎉 This issue has been resolved in version 4.2.18 🎉

    The release is available on GitHub release

    Your semantic-release bot 📦🚀

  7. added
    releasedIndicate that an issue has been resolved and released in a particular version of the product.
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugThis label could be used to identify issues that are caused by a defect in the product.releasedIndicate that an issue has been resolved and released in a particular version of the product.

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions