To always display the most recently modified articles at the top of your Twenty Twenty-Five site, add the following code to your theme’s functions.php file. This sorts posts by the last modified date instead of the original publish date on the home and archive pages.
Display Posts by Last Modified Date (Method 1)
phpfunction sort_posts_by_modified_date($query) {
if (!is_admin() && $query->is_main_query() && (is_home() || is_archive())) {
$query->set('orderby', 'modified');
$query->set('order', 'DESC');
}
}
add_action('pre_get_posts', 'sort_posts_by_modified_date');
How it works:
- This function hooks into WordPress’ main post query before it’s executed.
- It checks if you’re viewing the homepage or an archive page (not in the admin area).
- It changes the query to sort by the modified date, showing the most recently edited posts first.
To apply:
- Go to Appearance → Theme File Editor.
- Open your active theme’s functions.php.
- Paste the code above at the bottom and save.
Result:
Your blog and archives will now display posts in order of last modification date, keeping updated content at the top and letting readers find your freshest articles first.
Leave a Reply