Ticket #7457: 7457.diff

File 7457.diff, 3.5 KB (added by ryan, 4 years ago)

First draft, incomplete

  • wp-includes/post.php

     
    482482        } elseif ( ! empty($r['exclude']) ) 
    483483                $r['post__not_in'] = preg_split('/[\s,]+/',$r['exclude']); 
    484484 
     485        $r['caller_get_posts'] = true; 
     486 
    485487        $get_posts = new WP_Query; 
    486488        return $get_posts->query($r); 
    487489 
     
    750752} 
    751753 
    752754/** 
     755 * is_sticky() - Check if post is sticky 
     756 * 
     757 * {@internal Missing Long Description}} 
     758 * 
     759 * @package WordPress 
     760 * @subpackage Post 
     761 * @since 2.7 
     762 * 
     763 * @param int $post_id A post ID 
     764 * @return bool 
     765 */ 
     766function is_sticky($post_id) { 
     767        $stickies = get_option('sticky_posts'); 
     768 
     769        if ( !is_array($stickies) ) 
     770                return false; 
     771 
     772        if ( in_array($post_id, $stickies) ) 
     773                return true; 
     774 
     775        return false; 
     776} 
     777 
     778/** 
    753779 * sanitize_post() - Sanitize every post field 
    754780 * 
    755781 * {@internal Missing Long Description}} 
     
    847873        return $value; 
    848874} 
    849875 
     876function stick_post($post_id) { 
     877        $stickies = get_option('sticky_posts'); 
     878 
     879        if ( !is_array($stickies) ) 
     880                $stickies = array($post_id); 
     881 
     882        if ( ! in_array($post_id, $stickies) ) 
     883                $stickies[] = $post_id; 
     884 
     885        update_option('sticky_posts', $stickies); 
     886} 
     887 
     888function unstick_post($post_id) { 
     889        $stickies = get_option('sticky_posts'); 
     890 
     891        if ( !is_array($stickies) ) 
     892                return; 
     893 
     894        if ( ! in_array($post_id, $stickies) ) 
     895                return; 
     896 
     897        $offset = array_search($post_id, $stickies); 
     898        if ( false === $offset ) 
     899                return; 
     900 
     901        array_splice($stickies, $offset, 1); 
     902         
     903        update_option('sticky_posts', $stickies); 
     904} 
     905 
    850906/** 
    851907 * Count number of posts of a post type and is user has permissions to view. 
    852908 * 
  • wp-includes/query.php

     
    831831                $groupby = ''; 
    832832                $post_status_join = false; 
    833833 
     834                if ( !isset($q['caller_get_posts']) ) 
     835                        $q['caller_get_posts'] = false; 
     836 
    834837                if ( !isset($q['post_type']) ) { 
    835838                        if ( $this->is_search ) 
    836839                                $q['post_type'] = 'any'; 
     
    14601463                        $this->comment_count = count($this->comments); 
    14611464                } 
    14621465 
     1466                // Put sticky posts at the top of the posts array 
     1467                $sticky_posts = get_option('sticky_posts'); 
     1468                if ( $this->is_home && $page <= 1 && !empty($sticky_posts) && !$q['caller_get_posts'] ) { 
     1469                        $num_posts = count($this->posts); 
     1470                        $post_ids = array(); 
     1471                        $sticky_offset = 0; 
     1472                        // Loop over posts and relocate stickies to the front. 
     1473                        for ( $i = 0; $i < $num_posts; $i++ ) { 
     1474                                if ( in_array($this->posts[$i]->ID, $sticky_posts) ) { 
     1475                                        $sticky_post = $this->posts[$i]; 
     1476                                        // Remove sticky from current position 
     1477                                        array_splice($this->posts, $i, 1); 
     1478                                        // Move to front, after other stickies 
     1479                                        array_splice($this->posts, $sticky_offset, 0, array($sticky_post)); 
     1480                                        //Increment the sticky offset.  The next sticky goes here. 
     1481                                        $sticky_offset++; 
     1482                                        // Remove post from sticky posts array 
     1483                                        $offset = array_search($sticky_post->ID, $sticky_posts); 
     1484                                        array_splice($sticky_posts, $offset, 1); 
     1485                                } 
     1486                        } 
     1487 
     1488                        // Fetch sticky posts that weren't in the query results 
     1489                        // TODO Fetch all posts with one query 
     1490                        foreach ( $sticky_posts as $sticky_post ) { 
     1491                                $sticky_post = get_post($sticky_post); 
     1492                                array_splice($this->posts, $sticky_offset, 0, array($sticky_post)); 
     1493                                $sticky_offset++; 
     1494                        } 
     1495                } 
     1496 
    14631497                if ( !empty($limits) ) { 
    14641498                        $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' ); 
    14651499                        $this->found_posts = $wpdb->get_var( $found_posts_query );