'attachment', 'post_status' => 'inherit', 'fields' => 'ids', 'meta_query' => array( array( 'key' => '_wp_attachment_metadata', 'compare' => 'LIKE', 'value' => $file, ), ) ); $query = new WP_Query( $query_args ); if ( $query->have_posts() ) { foreach ( $query->posts as $attachment_id ) { $meta = wp_get_attachment_metadata( $attachment_id ); $original_file = basename( $meta['file'] ); $cropped_files = wp_list_pluck( $meta['sizes'], 'file' ); if ( $original_file === $file || in_array( $file, $cropped_files ) ) { return (int) $attachment_id; } } } } return false; }