WordPress.org

Make WordPress Core

Changeset 15973


Ignore:
Timestamp:
10/26/2010 01:50:38 PM (8 years ago)
Author:
ryan
Message:

Refactor wp_get_recent_posts to use get_posts(). Props blepoxp. fixes #14389

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r15970 r15973  
    22442244 *
    22452245 * @since 1.0.0
    2246  * @uses $wpdb
    2247  *
    2248  * @param int $num Optional, default is 10. Number of posts to get.
    2249  * @return array List of posts.
    2250  */
    2251 function wp_get_recent_posts($num = 10) {
    2252     global $wpdb;
    2253 
    2254     // Set the limit clause, if we got a limit
    2255     $num = (int) $num;
    2256     if ( $num ) {
    2257         $limit = "LIMIT $num";
    2258     }
    2259 
    2260     $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status IN ( 'draft', 'publish', 'future', 'pending', 'private' ) ORDER BY post_date DESC $limit";
    2261     $result = $wpdb->get_results($sql, ARRAY_A);
    2262 
    2263     return $result ? $result : array();
     2246 * @uses wp_parse_args()
     2247 * @uses get_posts()
     2248 *
     2249 * @param string $deprecated Deprecated.
     2250 * @param array $args Optional. Overrides defaults.
     2251 * @param string $output Optional.
     2252 * @return unknown.
     2253 */
     2254function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
     2255
     2256    if ( is_numeric( $args ) )
     2257        $args = array( 'numberposts' => absint( $args ) );
     2258
     2259    // Set default arguments
     2260    $defaults = array(
     2261        'numberposts' => 10, 'offset' => 0,
     2262        'category' => 0, 'orderby' => 'post_date',
     2263        'order' => 'DESC', 'include' => '',
     2264        'exclude' => '', 'meta_key' => '',
     2265        'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private',
     2266        'suppress_filters' => true
     2267    );
     2268   
     2269    $r = wp_parse_args( $args, $defaults );
     2270
     2271    $results = get_posts( $r );
     2272   
     2273    // Backward compatibility. Prior to 3.1 expected posts to be returned in array
     2274    if ( ARRAY_A == $output ){
     2275        foreach( $results as $key => $result ) {
     2276            $results[$key] = get_object_vars( $result );
     2277        }
     2278        return $results ? $results : array();
     2279    }
     2280
     2281    return $results ? $results : false;
     2282
    22642283}
    22652284
Note: See TracChangeset for help on using the changeset viewer.