WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
Beginner's Guide for WordPress / Start your WordPress Blog in minutes
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

WPBeginner» Blog» Tutorials» How to Display a List of Last Updated Posts in WordPress

How to Display a List of Last Updated Posts in WordPress

Last updated on June 3rd, 2015 by Editorial Staff
59 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
How to Display a List of Last Updated Posts in WordPress

At WPBeginner, we show last modified date instead of original published date for all of our articles. We think it is bad idea to remove dates from your WordPress blog posts. If you are using last modified date, then you might want to show a list of your recently updated posts on your site. In this article, we will show you how to display a list of last updated posts in WordPress.

Each time you update a post, WordPress stores the date and time of that update in the posts table as last updated date. We will show you how to create a custom WordPress query to list your most recently updated articles.

Copy and paste this code in a site-specific plugin or your theme’s functions.php file.


function wpb_lastupdated_posts() { 

// Query Arguments
$lastupdated_args = array(
'orderby' => 'modified',
'ignore_sticky_posts' => '1'
);

//Loop to display 5 recently updated posts
$lastupdated_loop = new WP_Query( $lastupdated_args );
$counter = 1;
$string .= '<ul>';
while( $lastupdated_loop->have_posts() && $counter < 5 ) : $lastupdated_loop->the_post();
$string .= '<li><a href="' . get_permalink( $lastupdated_loop->post->ID ) . '"> ' .get_the_title( $lastupdated_loop->post->ID ) . '</a> ( '. get_the_modified_date() .') </li>';
$counter++;
endwhile; 
$string .= '</ul>';
return $string;
wp_reset_postdata(); 
} 

//add a shortcode
add_shortcode('lastupdated-posts', 'wpb_lastupdated_posts');

That’s all. Now if you want to display last updated posts in your theme’s template files, then you can use it like this:

<?php 
if (function_exists(wpb_lastupdated_posts)) : 
wpb_lastupdated_posts();
endif;
?>

To display last updated posts in WordPress posts, pages, and widgets, then you can use the shortcode [lastupdated-posts].

There are many different ways to sort your articles in WordPress. Aside from the ascending, descending, and random order, you can also display posts by expiration date. With this article, you can now show posts by the last modified time.

How would you use this on your site? Are you displaying original published date or last modified date? Let us know by leaving a comment below.

59 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • Revealed: Why Building an Email List is so Important Today (6 Reasons)

    Revealed: Why Building an Email List is so Important Today (6 Reasons)

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

  • How to Start Your Own Podcast (Step by Step)

    How to Start Your Own Podcast (Step by Step)

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

33 Comments

Leave a Reply
  1. Mewaram jat says:
    Dec 29, 2020 at 11:14 pm

    I want to show the list of the Latest updated Products, instead of Posts. is it possible? How?

    Reply
    • WPBeginner Support says:
      Dec 30, 2020 at 9:24 am

      You would want to check with your eCommerce plugin for what options they would recommend as they normally have a widget or similar option.

      Reply
  2. Aditya says:
    Jun 19, 2020 at 1:31 pm

    How can I list more than 5 , say 30 post, recently modified post ?

    Reply
    • WPBeginner Support says:
      Jun 22, 2020 at 11:54 am

      If you are using the code from this article, you would change the 5 to a 30 for that :)

      Reply
  3. Sascha says:
    Jul 4, 2018 at 8:02 am

    Thank you for the instructions. That was exactly what I was looking for! Finally I can show the latest updates in my sidebar.

    Reply
  4. rren says:
    Nov 26, 2017 at 8:54 pm

    How do you include only updated posts and exclude pages?

    Reply
  5. Gwénaël says:
    Nov 14, 2016 at 7:48 am

    Hi,

    It works well, but it does not respect the content permissions of the page. It turns all the content visible to anybody…

    Reply
  6. Des says:
    Aug 4, 2016 at 6:59 am

    Hi,

    I followed you ‘How to Create a Custom WordPress Widget’, including ‘Creating a Site-Specific WordPress Plugin’ and that works perfectly. Great tutorial as I now have the ‘Hello world’ text displaying as a sidebar.

    But this one has me completely stumped. I followed the tutorial as best I could but it just displays my ‘Hello world’ text, never any list of updated posts.

    Where in my custom plugin file do I put the function wpb_lastupdated_posts()? Where do I put the ‘add_shortcode’ and where do I put the ‘if (function_exists(wpb_lastupdated_posts)) : ‘ statement?

    Many thanks,
    Des

    Reply
  7. Patrick Fortino says:
    May 9, 2016 at 9:08 pm

    Code works but displays only 1 post?

    Reply
  8. Gaurav Singh says:
    Apr 9, 2016 at 4:46 pm

    It is not working for genesis framework,

    Reply
  9. sagun khadka says:
    Mar 16, 2016 at 1:40 am

    How can i keep thumbnail of post when i keep it in widget ?

    Reply
  10. Benedito Carneiro says:
    Oct 22, 2015 at 9:15 am

    I created one site-specific plugin like you teached in your article What, Why, and How-To’s of Creating a Site-Specific WordPress Plugin, inserted function wpb_lastupdated_posts in the plugin, but had to add this line to the plugin to make text-widget shortcode work:

    add_filter(‘widget_text’, ‘do_shortcode’);

    Reply
    • WPBeginner Support says:
      Oct 22, 2015 at 11:21 am

      Great, thanks for sharing.

      Reply
  11. Aida says:
    Aug 3, 2015 at 3:38 am

    Thanks much, this article saved me. Code is working perfectly….

    Reply
  12. Tommy says:
    Jun 2, 2015 at 1:46 am

    Installed the code as stated. When using the short code- I add it to the bottom of the post. but when view the code- it puts the information on the top of the post. weird.

    Reply
    • WPBeginner Support says:
      Jun 3, 2015 at 2:50 pm

      @Tommy, thanks for notifying us. We have updated the code. Please use the new code and it will resolve the issue.

      Reply
  13. SebastienSerre says:
    Jul 29, 2014 at 9:04 am

    Hello

    Is it possible to have the last modificated page too?

    Many thx

    Reply
    • Marceli says:
      Aug 13, 2014 at 12:58 pm

      Same request here. I’m interested in listing recently updated Pages + showing the last_modified date. Ideally with some excerpt or number of characters from page.

      Reply
  14. jerik says:
    Jun 8, 2014 at 4:31 pm

    Great code, but is there anyway to get this exact same function but based on comments made by specific user (admin) and list the most recent comment made as the top of the threads in the list?

    Thanks!

    Reply
  15. Alberto Aguilar says:
    Mar 26, 2014 at 10:26 am

    Hi… great post…. Yet I was wondering if you could give me a hand on something: I need to take the picture and title of the last three published posts and arrange them in some kind of Gallery (Just to show the latest three published articles, Any idea if there’s a plugin for that, I assume I can use the code you just placed above, but Im not quite sure on the DB structure)

    Thanks in advance… =)

    Reply
  16. Mark Devlin says:
    Jan 24, 2014 at 3:06 am

    Hi, how do I add code to display the category and the user who made the update?

    Reply
  17. Norberto Vargas says:
    Nov 19, 2013 at 7:56 pm

    The second code?

    Reply
  18. Norberto Vargas says:
    Nov 19, 2013 at 7:55 pm

    Hello

    By the way nice article, so I have a doubt.

    Where to put this code:

    “”

    thank you

    Reply
    • WPBeginner Support says:
      Nov 19, 2013 at 10:31 pm

      In your template files where you want to display the list of last updated posts.

      Reply
  19. Pierre Guimond says:
    Oct 22, 2013 at 1:37 pm

    I do not know how to change the office location address on my WordPress web site. Try as I may, I cannot reach it to change it. http://www.mastheadpa.ca I would appreciate simple and straight answer. I can do most of the upkeep but that address change eludes me and i have not seen the key in the WP information and tutorials. help.

    Reply
    • WPBeginner Support says:
      Oct 22, 2013 at 7:41 pm

      @Pierre your office location is stored inside a widget. Inside your WordPress admin area, go to Appearance » Widgets. On your right hand column you will see a list of widgets currently in use on your site. Look for Footer Widget Area and there you will see Get in Touch widget which you can edit and save your changes.

      Reply
  20. Steve says:
    Oct 17, 2013 at 3:43 am

    Hi, love this article but I don’t quite get what is meant to be updated when you mention ‘use it like this:
    1

    Which file is this?

    Steve

    Reply
    • WPBeginner Support says:
      Oct 21, 2013 at 7:22 pm

      Some users might want to display last updated posts into different templates of their WordPress themes. Those users can use this code to add it. Other users can use the shortcode to add it in their posts, pages, and widgets.

      Reply
  21. erricgunawan says:
    Oct 16, 2013 at 11:28 pm

    Instead of using $counter for your loop, why don’t you just use the 'posts_per_page' attribute on the WP_Query args ?

    Mine goes like this:

    $lastupdated_args = array(
    ‘orderby’ => ‘modified’,
    ‘ignore_sticky_posts’ => 1,
    ‘posts_per_page’ => 5
    );

    Also, when I try the above code, it only give me one last modified post (not five as it should be).
    Wonder why …

    Reply
  22. Mary says:
    Oct 15, 2013 at 3:14 pm

    I love your articles but this one is way over my head. Are there any plugins for this? :)

    Sorry but I dont know the “innards” of the website and have broken my site when I tried a few things.

    It is a great idea though.Thanks for all you great work. Mary

    Reply
    • Rudd says:
      Oct 15, 2013 at 4:44 pm

      The code above is actually a ‘plugin’. Simply copy and paste the first code in functionality plugin. Then, there are two ways to display the list of posts, either using template tag or the easiest, using [lastupdated-posts] shortcode.

      Reply
      • Tommy says:
        Jun 2, 2015 at 1:47 am

        yes. but why does it add the content above the post wen using it as a short code?

        Reply
        • tommy says:
          Jun 2, 2015 at 1:52 am

          I want the content- then the shortcode info.

Leave a Reply Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
OptinMonster
OptinMonster
Convert website visitors into email subscribers. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2021 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2021)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2021)
    • SiteGround Reviews from 4464 Users & Our Experts (2021)
    • Bluehost Review from Real Users + Performance Stats (2021)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2021 – Step by Step Guide
Deals & Coupons (view all)
iPage Coupon
Get over 83% off on iPage web hosting and a free domain name.
Media Maestro Coupon
Get 30% OFF on Media Maestro WordPress media content management plugin.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).

Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
  • Growth Fund
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon
  • AIOSEO

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress Security by Sucuri.