YogeshChauhan.com
How to get recent posts in WordPress?
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

dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 4 How to use data-* Attributes in HTML? Mar 4 The substr() method in JavaScript and how it’s different from substring() Mar 4 A complete guide to add responsive YouTube videos using HTML and CSS Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript

You might also like these

How to make a web page look good on any device?HTMLHow to create a simple tab interaction using CSS?CSSHow to add navigation menu in Angular 9 app?AngularHow to Use ROLLUP Operator in SQL and MySQL?SQL/MySQLHow to vertically and horizontally align text and image block (without flex or grid) in CSS?CSSAngular 9 time clock update every minute, second, hourAngularWhy do we need HAVING Clause in SQL?SQL/MySQLWhat is XP (Extreme Programming) and When should it be used?MiscJavaScript Number MethodsJavaScriptAn Example of Cross-site Scripting (XSS) Attack in PHP and How to Avoid It?PHPHow to add onclick event to html elements dynamically using JavaScript?JavaScriptAggregate Functions Examples in Postgres for BeginnersPostgres