wp_get_recent_posts()

You are here:

wp_get_recent_posts( array $args = array()string $output = ARRAY_A )

Retrieve a number of recent posts.

Description Description

See also See also

  • get_posts()

Top ↑

Parameters Parameters

$args

(array) (Optional) Arguments to retrieve posts.

Default value: array()

$output

(string) (Optional) The required return type. One of OBJECT or ARRAY_A, which correspond to a WP_Post object or an associative array, respectively.

Default value: ARRAY_A


Top ↑

Return Return

(array|false) Array of recent posts, where the type of each element is determined by the $output parameter. Empty array on failure.


Top ↑

More Information More Information

Only the value of ARRAY_A is checked for $output. Any other value or constant passed will return an array of objects.

This function returns posts in an associative array (ARRAY_A) format which is compatible with WordPress versions below 3.1.

To get output similar to get_posts(), use OBJECT as the second parameter: wp_get_recent_posts( $args, OBJECT );


Top ↑

Source Source

File: wp-includes/post.php

3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
 
    if ( is_numeric( $args ) ) {
        _deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) );
        $args = array( 'numberposts' => absint( $args ) );
    }
 
    // Set default arguments.
    $defaults = array(
        'numberposts'      => 10,
        'offset'           => 0,
        'category'         => 0,
        'orderby'          => 'post_date',
        'order'            => 'DESC',
        'include'          => '',
        'exclude'          => '',
        'meta_key'         => '',
        'meta_value'       => '',
        'post_type'        => 'post',
        'post_status'      => 'draft, publish, future, pending, private',
        'suppress_filters' => true,
    );
 
    $parsed_args = wp_parse_args( $args, $defaults );
 
    $results = get_posts( $parsed_args );
 
    // Backward compatibility. Prior to 3.1 expected posts to be returned in array.
    if ( ARRAY_A == $output ) {
        foreach ( $results as $key => $result ) {
            $results[ $key ] = get_object_vars( $result );
        }
        return $results ? $results : array();
    }
 
    return $results ? $results : false;
 
}


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.
Was this article helpful?
Dislike 0
Views: 8