| | 3281 | * Explicitly add sticky posts to the query |
| | 3282 | */ |
| | 3283 | $sticky_posts = get_option('sticky_posts'); |
| | 3284 | if ( $this->is_home && !$q['ignore_sticky_posts'] && !empty( $sticky_posts ) ) { |
| | 3285 | // Grab only published sticky posts of our desired post type(s) |
| | 3286 | $stickies_query = new WP_Query( array( |
| | 3287 | 'post__in' => $sticky_posts, |
| | 3288 | 'post_type' => $post_type, |
| | 3289 | 'post_status' => 'publish', |
| | 3290 | 'fields' => 'ids', |
| | 3291 | 'ignore_sticky_posts' => true, |
| | 3292 | 'posts_per_page' => count( $sticky_posts ) |
| | 3293 | ) ); |
| | 3294 | |
| | 3295 | $queried_stickies = $stickies_query->posts; |
| | 3296 | |
| | 3297 | if( !empty( $queried_stickies ) ) { |
| | 3298 | $post__in_sticky = implode(',', array_map( 'absint', $queried_stickies ) ); |
| | 3299 | $where = "AND ((1=1 " . $where . ") OR {$wpdb->posts}.ID IN ($post__in_sticky))"; |
| | 3300 | $sticky_orderby = "FIELD( {$wpdb->posts}.ID, $post__in_sticky ) DESC"; |
| | 3301 | |
| | 3302 | if($orderby) { |
| | 3303 | $orderby = $sticky_orderby . ', ' . $orderby; |
| | 3304 | } |
| | 3305 | else { |
| | 3306 | $orderby = $sticky_orderby; |
| | 3307 | } |
| | 3308 | } |
| | 3309 | } |
| | 3310 | |
| | 3311 | /** |
| 3580 | | // Put sticky posts at the top of the posts array |
| 3581 | | $sticky_posts = get_option('sticky_posts'); |
| 3582 | | if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) { |
| 3583 | | $num_posts = count($this->posts); |
| 3584 | | $sticky_offset = 0; |
| 3585 | | // Loop over posts and relocate stickies to the front. |
| 3586 | | for ( $i = 0; $i < $num_posts; $i++ ) { |
| 3587 | | if ( in_array($this->posts[$i]->ID, $sticky_posts) ) { |
| 3588 | | $sticky_post = $this->posts[$i]; |
| 3589 | | // Remove sticky from current position |
| 3590 | | array_splice($this->posts, $i, 1); |
| 3591 | | // Move to front, after other stickies |
| 3592 | | array_splice($this->posts, $sticky_offset, 0, array($sticky_post)); |
| 3593 | | // Increment the sticky offset. The next sticky will be placed at this offset. |
| 3594 | | $sticky_offset++; |
| 3595 | | // Remove post from sticky posts array |
| 3596 | | $offset = array_search($sticky_post->ID, $sticky_posts); |
| 3597 | | unset( $sticky_posts[$offset] ); |
| 3598 | | } |
| 3599 | | } |
| 3601 | | // If any posts have been excluded specifically, Ignore those that are sticky. |
| 3602 | | if ( !empty($sticky_posts) && !empty($q['post__not_in']) ) |
| 3603 | | $sticky_posts = array_diff($sticky_posts, $q['post__not_in']); |
| 3604 | | |
| 3605 | | // Fetch sticky posts that weren't in the query results |
| 3606 | | if ( !empty($sticky_posts) ) { |
| 3607 | | $stickies = get_posts( array( |
| 3608 | | 'post__in' => $sticky_posts, |
| 3609 | | 'post_type' => $post_type, |
| 3610 | | 'post_status' => 'publish', |
| 3611 | | 'nopaging' => true |
| 3612 | | ) ); |
| 3613 | | |
| 3614 | | foreach ( $stickies as $sticky_post ) { |
| 3615 | | array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); |
| 3616 | | $sticky_offset++; |
| 3617 | | } |
| 3618 | | } |
| 3619 | | } |
| 3620 | | |