diff --git a/wp-includes/media.php b/wp-includes/media.php index da37f902c5..601538d8ea 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -4295,11 +4295,26 @@ function attachment_url_to_postid( $url ) { } $sql = $wpdb->prepare( - "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", + "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path ); - $post_id = $wpdb->get_var( $sql ); + $results = $wpdb->get_results( $sql ); + $post_id = null; + + if ( $results ) { + // Use the first available result, but prefer a case-sensitive match, if exists. + $post_id = reset( $results )->post_id; + + if ( count( $results ) > 1 ) { + foreach ( $results as $result ) { + if ( $path === $result->meta_value ) { + $post_id = $result->post_id; + break; + } + } + } + } /** * Filters an attachment id found by URL. diff --git a/wp-includes/version.php b/wp-includes/version.php index db2ffca22c..99aa6cf07a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.4-alpha-47009'; +$wp_version = '5.4-alpha-47010'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.