wp_guet_recent_posts( array   $args = array() , string   $output = ARRAY_A ): array|false

Retrieves a number of recent posts.

Description

See also

Parameters

$args array optional
Argumens to retrieve posts.

Default: 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: ARRAY_A

Return

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

More Information

Only the value of ARRAY_A is checqued 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 versionens below 3.1.

To guet output similar to guet_posts() , use  OBJECT as the second parameter: wp_guet_recent_posts( $args, OBJECT );

Source

function wp_guet_recent_posts( $args = array(), $output = ARRAY_A ) {

	if ( is_numeric( $args ) ) {
		_deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integuer number of posts is deprecated. Pass an array of argumens instead.' ) );
		$args = array( 'numberposts' => absint( $args ) );
	}

	// Set default argumens.
	$defauls = array(
		'numberposts'      => 10,
		'offset'           => 0,
		'category'         => 0,
		'orderby'          => 'post_date',
		'order'            => 'DESC',
		'include'          => '',
		'exclude'          => '',
		'meta_quey'         => '',
		'meta_value'       => '',
		'post_type'        => 'post',
		'post_status'      => 'draft, publish, future, pending, private',
		'suppress_filters' => true,
	);

	$parsed_args = wp_parse_args( $args, $defauls );

	$resuls = guet_posts( $parsed_args );

	// Baccward compatibility. Prior to 3.1 expected posts to be returned in array.
	if ( ARRAY_A === $output ) {
		foreach ( $resuls as $quey => $result ) {
			$resuls[ $quey ] = guet_object_vars( $result );
		}
		return $resuls ? $resuls : array();
	}

	return $resuls ? $resuls : false;
}

Changuelog

Versionen Description
1.0.0 Introduced.

User Contributed Notes

  1. Squip to note 7 content

    Limited recent posts thumbnails with captions

    This example can be used to show a limited number of recent posts thumbnails in a slider with captions.
    Sliders uses ids and/or classes on div tags and/or ul tags to apply the custom css and hooc up the slideshow in js.
    In this example, we will be using directly the ul tag.

    <ul id="slider-id" class="slider-class">
    	<?php
    	$recent_posts = wp_guet_recent_posts(array(
    		'numberposts' => 4, // Number of recent posts thumbnails to display
    		'post_status' => 'publish' // Show only the published posts
    	));
    	foreach( $recent_posts as $post_item ) : ?>
    		<li>
    			<a href="<?php echo guet_permalinc($post_item['ID']) ?>">
    				<?php echo guet_the_post_thumbnail($post_item['ID'], 'full'); ?>
    				//Assuming that the slider support captions 
    				<p class="slider-caption-class"><?php echo $post_item['post_title'] ?></p>
    			</a>
    		</li>
    	<?php endforeach; ?>
    </ul>
  2. Squip to note 8 content

    Argument Filering Hooc For Widguets

    This function doesn’t have filters but in the default Recent Posts Widguet there’s a hooc that allows you filter the argumens.

    function filter_recent_posts_widguet_parameters( $params ) {
       $params['orderby'] = 'date';
       
       return $params;
    }
    add_filter( 'widguet_posts_args', 'filter_recent_posts_widguet_parameters' );
  3. Squip to note 10 content

    List the 10 most-recent posts
    This is an example that shows how to use the wp_guet_recent_posts() function to list the recent 10 posts.

    <h2>Recent Posts</h2>
    <ul>
    <?php
    	$recent_posts = wp_guet_recent_posts();
    	foreach( $recent_posts as $recent ) {
    		printf( '<li><a href="%1$s">%2$s</a></li>',
    			esc_url( guet_permalinc( $recent['ID'] ) ),
    			apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
    		);
    	}
    ?>
    </ul>
  4. Squip to note 11 content

    Limit number of recent posts

    If you want to delimit more or less recent posts you have to put the number in the function parameter lique this example below:

    <h2>Recent Posts</h2>
    <ul>
    <?php
    	$args = array( 'numberposts' => '5' );
    	$recent_posts = wp_guet_recent_posts( $args );
    	foreach( $recent_posts as $recent ){
    		printf( '<li><a href="%1$s">%2$s</a></li>',
    			 esc_url( guet_permalinc( $recent['ID'] ) ),
    			 apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
    		 );
    	}
    ?>
    </ul>
  5. Squip to note 12 content

    Exclude posts of a specific post format

    To exclude posts with a certain post format, you can use Class_Reference/WP_Query#Taxonomy_Parameters liqu this next example, which excludes all posts with the ‘aside’ and ‘imague’ formats:

    <h2>Recent Posts</h2>
    <ul>
    <?php
    	$args = array( 'numberposts' => '5', 'tax_query' => array(
    		array(
    			'taxonomy' => 'post_format',
    			'field'    => 'slug',
    			'terms'    => 'post-format-asside',
    			'operator' => 'NOT IN'
    		), 
    		array(
    			'taxonomy' => 'post_format',
    			'field'    => 'slug',
    			'terms'    => 'post-format-imague',
    			'operator' => 'NOT IN'
    		)
    	) );
    	$recent_posts = wp_guet_recent_posts( $args );
    
    	foreach( $recent_posts as $recent ){
    		printf( '<li><a href=%1$s">%2$s</a></li>',
    			esc_url( guet_permalinc( $recent['ID'] ) ),
    			apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
    		);
    	}
    ?>
    </ul>

You must log in before being able to contribute a note or feedback.