wpseek.com
Bazujące na WordPress narzędzie wyszukiwania dla deweloperów i twórców motywów.



wp_get_recent_posts › WordPress Function

Od1.0.0
Przestarzałyn/a
› wp_get_recent_posts ( $args = array(), $output = ARRAY_A )
Parametry: (2)
  • (array|int) $args { Optional. Arguments to retrieve posts. See \get_posts and \WP_Query::parse_query for all available arguments. Those listed here have defaults that differ. Passing an integer number of posts is deprecated since 3.1.0. Default empty array. @type int $numberposts Total number of posts to retrieve. Accepts -1 for all. Default 10. @type int $offset The number of posts to offset before retrieval. Default 0. @type string|array $orderby Sort retrieved posts by parameter. Default 'post_date'. @type string|string[] $post_status A post status (string) or array of post statuses. Default 'draft, publish, future, pending, private'. }
    Wymagane: Nie
    Domyślny: array()
  • (string) $output Optional. The required return type. One of OBJECT or ARRAY_A, which correspond to a WP_Post object or an associative array, respectively. Default ARRAY_A.
    Wymagane: Nie
    Domyślny: ARRAY_A
Zobacz:
Powrót:
  • (array|false) Array of recent posts, where the type of each element is determined
    by the `$output` parameter. Empty array on failure.
Zdefiniowane na:
Codex:

Retrieves a number of recent posts.



Źródło

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'          => array(),
		'exclude'          => array(),
		'meta_key'         => '',
		'meta_value'       => '',
		'post_type'        => 'post',
		'post_status'      => 'draft, publish, future, pending, private',
		'suppress_filters' => true,
	);

	/** @var array{ numberposts: int, category: int|string, include: int[], exclude: int[], suppress_filters: bool, fields?: ''|'all', ... } $parsed_args */
	$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 ) {
		$posts = array();
		foreach ( $results as $key => $result ) {
			/** @var non-empty-array<string, mixed> $object_vars */
			$object_vars   = get_object_vars( $result );
			$posts[ $key ] = $object_vars;
		}
		return $posts;
	}

	return $results ? $results : false;
}