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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to convert a number rounding to a specified number of decimals in JavaScript?JavaScriptFull and Partial ROLLUP in Postgresql with ExamplesPostgresThe SQL UNION OperatorSQL/MySQLLearn to create profile card using HTML and CSSCSSA few HTML coding standards from WordPressHTML12 URLSearchParams methods in JavaScriptJavaScriptAdvanced Array Methods in JavaScript (with examples)JavaScriptHow to get query string values in JavaScript? (URL Manipulation using URLSearchParams)JavaScriptCREATE DATABASE in PostgreSQLPostgresHow to use Context in React?React6 Different Functions to Sort Arrays in PHPPHPHow to get previous days or next days in PHP?PHP