Index: media.php
===================================================================
--- media.php	(revision 24556)
+++ media.php	(working copy)
@@ -1080,30 +1080,54 @@
  * Retrieves the current attachment object from the $post global.
  *
  * @since 2.5.0
+ * @uses _previous_image_posts_where_filter_once()
+ * @uses _next_image_posts_where_filter_once()
  *
  * @param bool $prev Optional. Default is true to display previous link, false for next.
  */
 function adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) {
+	$adjacent = $prev ? 'previous' : 'next';
+	$order = $prev ? 'DESC' : 'ASC';
 	$post = get_post();
-	$attachments = array_values( get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) ) );
+	add_filter( 'posts_where', "_{$adjacent}_image_posts_where_filter_once" );
+	$attachment = array_pop( get_children( array(
+		'post_parent' => $post->post_parent,
+		'post_status' => 'inherit',
+		'post_type' => 'attachment',
+		'post_mime_type' => 'image',
+		'orderby' => "menu_order $order, ID $order",
+		'posts_per_page' => 1,
+		'suppress_filters' => false,
+	) ) );
 
-	foreach ( $attachments as $k => $attachment )
-		if ( $attachment->ID == $post->ID )
-			break;
-
-	$k = $prev ? $k - 1 : $k + 1;
-
 	$output = $attachment_id = null;
-	if ( isset( $attachments[ $k ] ) ) {
-		$attachment_id = $attachments[ $k ]->ID;
+	if ( isset( $attachment ) ) {
+		$attachment_id = $attachment->ID;
 		$output = wp_get_attachment_link( $attachment_id, $size, true, false, $text );
 	}
 
-	$adjacent = $prev ? 'previous' : 'next';
 	echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text );
 }
 
 /**
+ * Append to WHERE clause for selecting only the previous image.
+ */
+function _previous_image_posts_where_filter_once( $where ) {
+	remove_filter( 'posts_where', __FUNCTION__ );
+	$post = get_post();
+	return "$where AND (menu_order < $post->menu_order OR (menu_order = $post->menu_order AND ID < $post->ID))";
+}
+
+/**
+ * Append to WHERE clause for selecting only the next image.
+ */
+function _next_image_posts_where_filter_once( $where ) {
+	remove_filter( 'posts_where', __FUNCTION__ );
+	$post = get_post();
+	return "$where AND (menu_order > $post->menu_order OR (menu_order = $post->menu_order AND ID > $post->ID))";
+}
+
+/**
  * Retrieve taxonomies attached to the attachment.
  *
  * @since 2.5.0
