How to Display the Most Recently Updated Posts and Pages in WordPress

Home » Snippets » How to Display the Most Recently Updated Posts and Pages in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking for a way to display the most recently updated posts and pages on your WordPress website? Keeping your content fresh and up-to-date is crucial for engaging your audience and maintaining the credibility of your website. In this article, we’ll show you how to easily showcase the most recently updated posts and pages on your site using WordPress and without the need for any coding. Whether you want to highlight the latest news updates, product releases, or any other type of content changes, this tutorial will guide you through the process step-by-step. Let’s dive in and ensure that your visitors always have access to the most recent content on your WordPress site.

					function wpturbo_display_recently_updated_posts($num_posts) {
    $args = array(
        'post_type'   => array( 'post', 'page' ),
        'orderby'     => 'modified',
        'order'       => 'DESC',
        'numberposts' => $num_posts,
    );
   
    $recent_posts = get_posts( $args );

    foreach( $recent_posts as $post ) {
        setup_postdata( $post );
        echo '<li><a href="' . get_permalink( $post->ID ) . '">' . get_the_title( $post->ID ) . '</a></li>';
    }

    wp_reset_postdata();
}

add_shortcode( 'wpturbo_recently_updated', 'wpturbo_display_recently_updated_posts' );
				

The code snippet above defines a function called wpturbo_display_recently_updated_posts() which is responsible for displaying a list of the most recently updated posts and pages in WordPress. This function takes one parameter, $num_posts, which represents the number of posts to display.

Inside the function, we define an array called $args which includes all the necessary parameters to query the posts. We specify the post types to include as post and page, and we sort the results by the modified date in descending order. We also specify the number of posts to retrieve using the numberposts parameter.

Then, we use the get_posts() function to retrieve the recently updated posts based on the defined arguments. The results are stored in the $recent_posts variable.

Next, we loop through each post in the $recent_posts array using a foreach loop. Inside the loop, we use the setup_postdata() function to set up the global post data for each post, allowing us to access the post’s properties and functions.

For each post, we use the get_permalink() function to retrieve the URL of the post, and the get_the_title() function to retrieve the title of the post. We then output an HTML list item (<li>) with an anchor tag (<a>) that includes the post’s URL and title.

After the loop, we use the wp_reset_postdata() function to reset the global post data to the main loop’s post.

To make the recently updated posts functionality easily accessible, we add a shortcode using the add_shortcode() function. The shortcode wpturbo_recently_updated is associated with the wpturbo_display_recently_updated_posts() function, so whenever the shortcode is used in a post or page, it will call the function and display the list of recently updated posts.

This way, you can simply insert [wpturbo_recently_updated] into your content editor to display the most recently updated posts and pages on your website.

Register an account to save your snippets or go Pro to get more features.