Yogesh Chauhan's Blog

How to get recent posts in WordPress?

in WordPress on February 8, 2021

We can use wp_get_recent_posts() function to retrieve number of recent posts.

Syntax


wp_get_recent_posts($args, $output)

Both of the parameters are optional.

$args parameter is the arguments we use to retrieve the posts. We can pass it as an array(). $output is a string. We can also pass it as an object or an associative array.

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

The function will return an array of recent posts where the type of each element is determined by the $output parameter. It will return an empty array on failure.

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

Examples

List the 10 most-recent posts



<h2>Recent Posts</h2>
<ul>
<?php
    $recent_posts = wp_get_recent_posts();
    foreach( $recent_posts as $recent ) {
        printf( '<li><a href="%1$s">%2$s</a></li>',
            esc_url( get_permalink( $recent['ID'] ) ),
            apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
        );
    }
?>
</ul>


Limit number of recent posts



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


Exclude posts of a specific post format



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


Source: WordPress


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Is PHP still good for back-end programming?PHPLearn to make a responsive gallery using CSS Grid and without media queriesCSSReverse a String in JavaScriptJavaScriptFULL OUTER JOIN in PostgresPostgresHow to detect if browser supports WebP format on server side PHP script?PHPThe fundamental difference between HAVING and WHERE clauses in PostgresPostgresGeneral concepts in DrupalDrupalHow to Draw a Text Image using JavaScript?JavaScriptHow to use Context in React?ReactWhat is ECMAScript?JavaScript4 ways to create Date Objects in JavaScriptJavaScriptHow to change HTML content on click using JavaScript?JavaScript