Ticket #21986: sticky_posts_query.patch

File sticky_posts_query.patch, 5.0 KB (added by martinshopland, 8 months ago)
  • wp-includes/query.php

    From d01ad4ca08f12a00a39407a9e7927a18bd17255e Mon Sep 17 00:00:00 2001
    From: Martin Shopland <martin.shopland@redbrickstudios.co.uk>
    Date: Mon, 24 Sep 2012 19:20:11 +0100
    Subject: [PATCH] New sticky posts functionality rolled into core.
    
    ---
     wp-includes/query.php |   66 +++++++++++-------------------------------------
     1 files changed, 15 insertions(+), 51 deletions(-)
    
    diff --git a/wp-includes/query.php b/wp-includes/query.php
    index 5f6ccdd..f7fe62a 100644
    a b class WP_Query { 
    25582558                                $where = "AND 0"; 
    25592559                } 
    25602560 
    2561                 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); 
     2561                // Sticky Posts 
     2562                // Treated as separate clause so as not to upset existing plugins. 
     2563                $sticky_posts = get_option('sticky_posts'); 
     2564                if ( $this->is_home && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) { 
     2565                        $sticky = '(' . $wpdb->posts . '.ID IN (' . implode(',', $sticky_posts) . ')) DESC'; 
     2566                } else { 
     2567                        $sticky = ''; 
     2568                } 
     2569 
     2570                $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits', 'sticky' ); 
    25622571 
    25632572                // Apply post-paging filters on where and join. Only plugins that 
    25642573                // manipulate paging queries should use these hooks. 
    class WP_Query { 
    25702579                        $distinct       = apply_filters_ref_array( 'posts_distinct',    array( $distinct, &$this ) ); 
    25712580                        $limits         = apply_filters_ref_array( 'post_limits',               array( $limits, &$this ) ); 
    25722581                        $fields         = apply_filters_ref_array( 'posts_fields',              array( $fields, &$this ) ); 
     2582                        $sticky         = apply_filters_ref_array( 'posts_sticky',              array( $sticky, &$this ) ); 
    25732583 
    25742584                        // Filter all clauses at once, for convenience 
    25752585                        $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) ); 
    class WP_Query { 
    25892599                        $distinct       = apply_filters_ref_array( 'posts_distinct_request',    array( $distinct, &$this ) ); 
    25902600                        $fields         = apply_filters_ref_array( 'posts_fields_request',              array( $fields, &$this ) ); 
    25912601                        $limits         = apply_filters_ref_array( 'post_limits_request',               array( $limits, &$this ) ); 
     2602                        $sticky         = apply_filters_ref_array( 'posts_sticky_request',              array( $sticky, &$this ) ); 
    25922603 
    25932604                        // Filter all clauses at once, for convenience 
    25942605                        $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); 
    class WP_Query { 
    25962607                                $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 
    25972608                } 
    25982609 
     2610                if ( !empty($sticky)) 
     2611                        $orderby = empty($orderby) ? $sticky : $sticky . ',' . $orderby; 
     2612 
    25992613                if ( ! empty($groupby) ) 
    26002614                        $groupby = 'GROUP BY ' . $groupby; 
    26012615                if ( !empty( $orderby ) ) 
    class WP_Query { 
    27032717                                $this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this )); 
    27042718                } 
    27052719 
    2706                 // Put sticky posts at the top of the posts array 
    2707                 $sticky_posts = get_option('sticky_posts'); 
    2708                 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) { 
    2709                         $num_posts = count($this->posts); 
    2710                         $sticky_offset = 0; 
    2711                         // Loop over posts and relocate stickies to the front. 
    2712                         for ( $i = 0; $i < $num_posts; $i++ ) { 
    2713                                 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) { 
    2714                                         $sticky_post = $this->posts[$i]; 
    2715                                         // Remove sticky from current position 
    2716                                         array_splice($this->posts, $i, 1); 
    2717                                         // Move to front, after other stickies 
    2718                                         array_splice($this->posts, $sticky_offset, 0, array($sticky_post)); 
    2719                                         // Increment the sticky offset. The next sticky will be placed at this offset. 
    2720                                         $sticky_offset++; 
    2721                                         // Remove post from sticky posts array 
    2722                                         $offset = array_search($sticky_post->ID, $sticky_posts); 
    2723                                         unset( $sticky_posts[$offset] ); 
    2724                                 } 
    2725                         } 
    2726  
    2727                         // If any posts have been excluded specifically, Ignore those that are sticky. 
    2728                         if ( !empty($sticky_posts) && !empty($q['post__not_in']) ) 
    2729                                 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']); 
    2730  
    2731                         // Fetch sticky posts that weren't in the query results 
    2732                         if ( !empty($sticky_posts) ) { 
    2733                                 $stickies__in = implode(',', array_map( 'absint', $sticky_posts )); 
    2734                                 // honor post type(s) if not set to any 
    2735                                 $stickies_where = ''; 
    2736                                 if ( 'any' != $post_type && '' != $post_type ) { 
    2737                                         if ( is_array( $post_type ) ) { 
    2738                                                 $post_types = join( "', '", $post_type ); 
    2739                                         } else { 
    2740                                                 $post_types = $post_type; 
    2741                                         } 
    2742                                         $stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')"; 
    2743                                 } 
    2744  
    2745                                 $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" ); 
    2746                                 foreach ( $stickies as $sticky_post ) { 
    2747                                         // Ignore sticky posts the current user cannot read or are not published. 
    2748                                         if ( 'publish' != $sticky_post->post_status ) 
    2749                                                 continue; 
    2750                                         array_splice($this->posts, $sticky_offset, 0, array($sticky_post)); 
    2751                                         $sticky_offset++; 
    2752                                 } 
    2753                         } 
    2754                 } 
    2755  
    27562720                if ( !$q['suppress_filters'] ) 
    27572721                        $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) ); 
    27582722