Make WordPress Core

Ticket #9300: 0001-Fix-pagination-with-sticky-posts-fixes-9300.patch

File 0001-Fix-pagination-with-sticky-posts-fixes-9300.patch, 3.3 KB (added by JasonWoof, 10 years ago)

patch to fix the bug (in version 3.2.1)

  • wp-includes/query.php

    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 b class WP_Query { 
    23652365                                $orderby .= " {$q['order']}";
    23662366                }
    23672367
     2368                $sticky_posts = get_option('sticky_posts');
     2369                if ( $this->is_home && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
     2370                        if ( ! empty( $orderby ) )
     2371                                $orderby = ', ' . $orderby;
     2372                        $orderby = 'wp_posts.ID in(' . implode(',', $sticky_posts) . ') DESC' . $orderby;
     2373                }
     2374
     2375
    23682376                if ( is_array( $post_type ) ) {
    23692377                        $post_type_cap = 'multiple_post_type';
    23702378                } else {
    class WP_Query { 
    26782686                                $this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));
    26792687                }
    26802688
    2681                 // Put sticky posts at the top of the posts array
    2682                 $sticky_posts = get_option('sticky_posts');
    2683                 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
    2684                         $num_posts = count($this->posts);
    2685                         $sticky_offset = 0;
    2686                         // Loop over posts and relocate stickies to the front.
    2687                         for ( $i = 0; $i < $num_posts; $i++ ) {
    2688                                 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
    2689                                         $sticky_post = $this->posts[$i];
    2690                                         // Remove sticky from current position
    2691                                         array_splice($this->posts, $i, 1);
    2692                                         // Move to front, after other stickies
    2693                                         array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
    2694                                         // Increment the sticky offset.  The next sticky will be placed at this offset.
    2695                                         $sticky_offset++;
    2696                                         // Remove post from sticky posts array
    2697                                         $offset = array_search($sticky_post->ID, $sticky_posts);
    2698                                         unset( $sticky_posts[$offset] );
    2699                                 }
    2700                         }
    2701 
    2702                         // If any posts have been excluded specifically, Ignore those that are sticky.
    2703                         if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
    2704                                 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
    2705 
    2706                         // Fetch sticky posts that weren't in the query results
    2707                         if ( !empty($sticky_posts) ) {
    2708                                 $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
    2709                                 // honor post type(s) if not set to any
    2710                                 $stickies_where = '';
    2711                                 if ( 'any' != $post_type && '' != $post_type ) {
    2712                                         if ( is_array( $post_type ) ) {
    2713                                                 $post_types = join( "', '", $post_type );
    2714                                         } else {
    2715                                                 $post_types = $post_type;
    2716                                         }
    2717                                         $stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
    2718                                 }
    2719 
    2720                                 $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
    2721                                 foreach ( $stickies as $sticky_post ) {
    2722                                         // Ignore sticky posts the current user cannot read or are not published.
    2723                                         if ( 'publish' != $sticky_post->post_status )
    2724                                                 continue;
    2725                                         array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
    2726                                         $sticky_offset++;
    2727                                 }
    2728                         }
    2729                 }
    2730 
    27312689                if ( !$q['suppress_filters'] )
    27322690                        $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
    27332691