Make WordPress Core

Opened 4 years ago

Last modified 3 years ago

#51058 new defect (bug)

attachment_url_to_postid does not retrieve post ID of really large images

Reported by: littlerchicken's profile littler.chicken Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.3
Component: Media Keywords: has-patch needs-testing
Focuses: Cc:


Since WordPress now sets the _wp_attached_file meta value for very large images to use the scaled image:




any function which uses attachment_url_to_postid on a large image URL will not properly retrieve the image's post ID.

Use case: I use this function after removing intermediate image size strings to get the post ID of the uploaded image. I'm currently getting around this issue by inserting -scaled into the image URL and running the function again if a post ID is not returned, but I would prefer if WP could account for this, please.

Alternatively, if the WordPress function could manage the intermediate size conversion on its own, that would also resolve the issue by removing the need to remove it in the first place.

Attachments (1)

attachment_url_to_postid.patch (2.7 KB) - added by jdorner 3 years ago.
Patch to address making attachment_url_to_postid work for all attachment sizes and for permalinks and for "scaled" images

Download all attachments as: .zip

Change History (8)

#1 @Mista-Flo
4 years ago

Can you give steps to reproduce please? And any screenshots if it can help.

#2 @littler.chicken
4 years ago

To reproduce, upload a very large image to your site (wider than 2560px). Get the URL of the original uploaded image.

Add code to dump out some information similar to this:

	function( $content ) {
		// Enter the URL of the original image
		var_dump( attachment_url_to_postid( 'https://nightly.local/wp-content/uploads/2020/03/roman-kraft-197672.jpg' ) );
		// Now add -scaled to the original image name
		var_dump( attachment_url_to_postid( 'https://nightly.local/wp-content/uploads/2020/03/roman-kraft-197672-scaled.jpg' ) );
		return $content;

For the first, WordPress will output 0 as no image exists with that filename. The second will return the image ID, because that's the filename assigned to the _wp_attached_file meta key (ref:

When I use this function, it's in the context of parsing the RSS feed, at which point the original ID is not available, but I'm retrieving it from the database so that the image can be replaced with a smaller copy. Currently, without a secondary check for the -scaled image, the replacement fails for very large images.

#3 @metalandcoffee
4 years ago

  • Version changed from trunk to 5.3

I was able to reproduce this issue.

Appending -scaled to an oversized PNG images was introduced in 5.3 (

The function attachment_url_to_postid is simply doing a check against the database with whichever url you give it:

$sql = $wpdb->prepare(
"SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path );

So it makes sense that it'll return nothing since the file was immediately renamed upon upload.

#4 @celloexpressions
3 years ago

This should be fixed similarly to #41816.

3 years ago

Patch to address making attachment_url_to_postid work for all attachment sizes and for permalinks and for "scaled" images

#5 @jdorner
3 years ago

  • Keywords has-patch needs-testing added

#7 @digitalswing
3 years ago

In the case where the image is 3bbd5767-f4be-33f8-b124-ee91953caaa6.jpg the $filename = \end( $path_parts ); part of @jdorner 's patch returns ee91953caaa6.jpg.

Note: See TracTickets for help on using tickets.