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 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #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

#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 #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
How to check if a link has http or https in it in JavaScript?JavaScriptForcing the domain to serve securely using HTTPSMiscellaneousCONCAT and CONCAT_WS Functions in PostgresPostgresList of all Pseudo Elements in CSSCSSHow to get query string values in JavaScript? (URL Manipulation using URLSearchParams)JavaScriptHow to Use ROLLUP Operator in SQL and MySQL?SQL/MySQLHow to change value of a span tag using a reference from another div using jQuery?jQueryHow to show slider value in HTML5 range input using JavaScript?HTMLCreate a galley with overlapping images using CSS gridCSSrandom function in SCSS (Sass)SCSSHow to solve “framework not found” error in Swift?SwiftUse SwiperJS to create mobile touch sliders fastMiscellaneous