I’m updating a bunch of older posts on my site, and I’m trying to see which posts have gone the longest without modifications. Unfortunately, WordPress’s list of posts and pages can only be sorted by “Date”, which means the date of publication. There’s no in-built way to create a new column called “Modified Date” and sort by that.
So here are two ways to sort WordPress posts and pages by the “modified date” instead of the publication date. The first one is easy and doesn’t require any coding. The second method includes code to paste into your theme’s functions.php.
Method 1: The Easy Way – No Coding Required
If you’re not looking for a permanent solution and want the WordPress admin dashboard to sort your posts or pages by the modified date once in a while, then there’s no need to use a plugin or fiddle around with code. You need to change the URL that appears when you sort by “Date”.
Navigate to the list of posts or pages, and click the “Date” column, so that it sorts the posts by the date of publication. Now modify the URL and replace “date” with “modified” and change the order to “asc” instead of “desc” like this:
https://www.example.com/blog/wp-admin/edit.php?orderby=modified&order=asc
Naturally, you want to replace “example.com/blog” with the name of your WordPress website. Loading the above URL will sort the entries on the current page by the modified date. Setting the “order” parameter to “asc” will give you the oldest posts to have been modified, and “desc” will show you the most recently modified posts first.
Neat huh?
Method 2: Adding Code to Sort by the Modified Date
If you’re looking for a more permanent solution, add the following code to your theme’s functions.php file:
function sort_by_modified( $wp_query ) {
global $pagenow;
if ( is_admin() && 'edit.php' == $pagenow) {
$wp_query->set( 'orderby', 'modified' );
$wp_query->set( 'order', 'ASC' );
}
}
add_filter('pre_get_posts', 'sort_by_modified' );
If you don’t know how to add code to WordPress like this, read my tutorial on how to add code to WordPress without a plugin.
The above code changes the query on the backend of the WordPress admin that lists the posts or pages and shows a new one instead. True, the interface becomes a bit misleading because you get the impression that it’s sorting posts by the date instead of the modified date, but who cares? I’m not going to figure out how to change the name of the date column for cosmetic reasons!
Sorting by the Modified Date is Critical for SEO Maintenance
I’ve just realized that I have several posts on my site WP-Tweaks.com that are several years old. Most of these are tutorials, so their information is still relevant, but some things can be refreshed. The screenshots with better resolution reflect the changes in the WordPress editor with Gutenberg, new image WEBP formats, product descriptions, and titles.
For this kind of SEO task, sorting the posts in order of the last modified date is a godsend. I prefer to use the URL method of sorting the posts since I only need to do it once in a while, and I think that will be true for most people. After all, why change something permanently when the requirement is so niche?
You need to use the coding solution only if you have a compelling reason to always sort the posts by the modified date. I hope you found this tutorial helpful!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
jack says
function sort_by_modified( $wp_query ) {
global $pagenow;
if ( is_admin() && ‘edit.php’ == $pagenow) {
$wp_query->set( ‘orderby’, ‘modified’ );
$wp_query->set( ‘order’, ‘ASC’ );
}
}
add_filter(‘pre_get_posts’, ‘sort_by_modified’ );
this is only for “if is admin” … what if you want to make it show the order by modified for anyone who comes on your site, register and unregistered users.
CornElDos says
I would think just removing is_admin would do it
function sort_by_modified( $wp_query ) {
global $pagenow;
if ( ‘edit.php’ == $pagenow) {
$wp_query->set( ‘orderby’, ‘modified’ );
$wp_query->set( ‘order’, ‘ASC’ );
}
}
add_filter(‘pre_get_posts’, ‘sort_by_modified’ );
Ljubomir says
Thank you. This should be first result on google serach
Andrew Karr says
You code works perfectly for sorting page as they are displayed in the admin edit screen. How do you get it to do it on the front end, where you are showing posts to general users?
Deborah Mundorff says
Thank you so much. I was considering paying $90 for a plugin with this feature.