Skip to content

Commit 04b6bdc

Browse files
Copilotswissspidy
andcommitted
Fix duplicate detection for WP 5.3+ scaled images via _wp_original_image_file
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent ae70425 commit 04b6bdc

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

src/Media_Command.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,11 @@ private function make_copy( $path ) {
720720
* Finds an existing attachment whose basename matches the given filename.
721721
*
722722
* Searches the `_wp_attached_file` post meta, which stores the path relative to
723-
* the uploads directory (e.g. '2026/03/image.jpg' or just 'image.jpg'). Matches
724-
* the first attachment found when multiple files share the same basename across
725-
* different upload subdirectories.
723+
* the uploads directory (e.g. '2026/03/image.jpg' or just 'image.jpg'). Also
724+
* checks `_wp_original_image_file` (absolute path, WP 5.3+) to handle images
725+
* that were scaled down on upload (stored as 'image-scaled.jpg') but whose
726+
* original filename is still 'image.jpg'. Matches the first attachment found
727+
* when multiple files share the same basename across different subdirectories.
726728
*
727729
* @param string $basename Filename basename to search for (e.g. 'image.jpg').
728730
* @return int|false Attachment ID if found, false otherwise.
@@ -746,7 +748,7 @@ private function find_duplicate_attachment( $basename ) {
746748
ON p.ID = pm.post_id
747749
WHERE p.post_type = 'attachment'
748750
AND p.post_status != 'trash'
749-
AND pm.meta_key = '_wp_attached_file'
751+
AND pm.meta_key IN ('_wp_attached_file', '_wp_original_image_file')
750752
AND ( pm.meta_value = %s OR RIGHT(pm.meta_value, %d) = %s )
751753
LIMIT 1",
752754
$basename,

0 commit comments

Comments
 (0)