YogeshChauhan . com

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

amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to change the Login Logo in WordPress?WordPressWhat is iFrame in HTML? Why do we need it?HTMLHow to activate and deactivate hooks in a WordPress plugin?WordPressHow to implement NSNumberFormatter in Swift for currency?SwiftHigher Order Functions in JavaScript with ExamplesJavaScriptHow does AdSense calculate page loading time?JavaScriptHTML Copyright SymbolHTMLAlways add associated labels to your Form elementsUI/UXHow to create a sidebar using pure CSS?CSSWhat is the difference between Loosely Typed Language and Strongly Typed Language?MiscThe Difference Between the echo and print Commands in PHPPHPPostgreSQL BETWEENPostgres