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' ); |
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 | | |