From 411ebad7fdeddc85f88f55b7f2a396251e23e67e Mon Sep 17 00:00:00 2001
From: Jason Woofenden <jason@jasonwoof.com>
Date: Mon, 29 Apr 2013 02:26:37 -0400
Subject: [PATCH] Fix pagination with sticky posts (fixes #9300)

---
 wp-includes/query.php |   58 +++++++------------------------------------------
 1 file changed, 8 insertions(+), 50 deletions(-)

diff --git a/wp-includes/query.php b/wp-includes/query.php
index d70348c..faf376d 100644
--- a/wp-includes/query.php
+++ b/wp-includes/query.php
@@ -2365,6 +2365,14 @@ class WP_Query {
 				$orderby .= " {$q['order']}";
 		}
 
+		$sticky_posts = get_option('sticky_posts');
+		if ( $this->is_home && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
+			if ( ! empty( $orderby ) )
+				$orderby = ', ' . $orderby;
+			$orderby = 'wp_posts.ID in(' . implode(',', $sticky_posts) . ') DESC' . $orderby;
+		}
+
+
 		if ( is_array( $post_type ) ) {
 			$post_type_cap = 'multiple_post_type';
 		} else {
@@ -2678,56 +2686,6 @@ class WP_Query {
 				$this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));
 		}
 
-		// Put sticky posts at the top of the posts array
-		$sticky_posts = get_option('sticky_posts');
-		if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
-			$num_posts = count($this->posts);
-			$sticky_offset = 0;
-			// Loop over posts and relocate stickies to the front.
-			for ( $i = 0; $i < $num_posts; $i++ ) {
-				if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
-					$sticky_post = $this->posts[$i];
-					// Remove sticky from current position
-					array_splice($this->posts, $i, 1);
-					// Move to front, after other stickies
-					array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
-					// Increment the sticky offset.  The next sticky will be placed at this offset.
-					$sticky_offset++;
-					// Remove post from sticky posts array
-					$offset = array_search($sticky_post->ID, $sticky_posts);
-					unset( $sticky_posts[$offset] );
-				}
-			}
-
-			// If any posts have been excluded specifically, Ignore those that are sticky.
-			if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
-				$sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
-
-			// Fetch sticky posts that weren't in the query results
-			if ( !empty($sticky_posts) ) {
-				$stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
-				// honor post type(s) if not set to any
-				$stickies_where = '';
-				if ( 'any' != $post_type && '' != $post_type ) {
-					if ( is_array( $post_type ) ) {
-						$post_types = join( "', '", $post_type );
-					} else {
-						$post_types = $post_type;
-					}
-					$stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
-				}
-
-				$stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
-				foreach ( $stickies as $sticky_post ) {
-					// Ignore sticky posts the current user cannot read or are not published.
-					if ( 'publish' != $sticky_post->post_status )
-						continue;
-					array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
-					$sticky_offset++;
-				}
-			}
-		}
-
 		if ( !$q['suppress_filters'] )
 			$this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
 
-- 
1.7.10.4

