WordPress – Sort Posts by Modification Date

Overview

By default WordPress sorts posts by creation date. Under such default setting, changes made to an existing post might not be noticed by blog readers. To order your posts by modification date, you can use the code snippet below courtesy of Sebastian’s blog article.

function order_posts_by_mod_date($orderby) {   
  if  (is_home() || is_archive() || is_feed()) {     
    $orderby = "post_modified_gmt DESC";   
  }   
  return $orderby; 
} 
add_filter('posts_orderby', 'order_posts_by_mod_date', 999);

The code snippet is placed in your theme’s functions.php file.

Editing the functions.php file

Let’s take an example of a WordPress site that is hosted as a virtual machine (VM) image on Google Cloud Platform. To access the functions.php file, you must SSH into the VM. Then, navigate to the folder structure that contains the functions.php file as shown below.

$ cd /var/www/html/wp-content/themes/geodesic/
$ ls -la
total 672
drwxr-xr-x 8 www-data www-data   4096 Apr 28 18:26 .
drwxr-xr-x 5 www-data www-data   4096 Apr 15 16:25 ..
-rw-r--r-- 1 www-data www-data   1125 Apr  5 19:16 404.php
-rw-r--r-- 1 www-data www-data   2105 Apr  5 19:16 archive.php
-rw-r--r-- 1 www-data www-data   3434 Apr  5 19:16 comments.php
drwxr-xr-x 2 www-data www-data   4096 Apr  5 19:16 css
drwxr-xr-x 2 www-data www-data   4096 Apr  5 19:16 fonts
-rw-r--r-- 1 www-data www-data    486 Apr  5 19:16 footer.php
-rw-r--r-- 1 www-data www-data   5987 Apr 28 18:26 functions.php
-rw-r--r-- 1 www-data www-data    676 Apr  5 19:16 header.php
-rw-r--r-- 1 www-data www-data   1530 Apr  5 19:16 image.php
drwxr-xr-x 2 www-data www-data   4096 Apr  5 19:16 inc
-rw-r--r-- 1 www-data www-data   2211 Apr  5 19:16 index.php
drwxr-xr-x 2 www-data www-data   4096 Apr  5 19:16 js
drwxr-xr-x 2 www-data www-data   4096 Apr  5 19:16 languages
-rw-r--r-- 1 www-data www-data   1266 Apr  5 19:16 page.php
-rw-r--r-- 1 www-data www-data   3915 Apr  5 19:16 readme.txt
-rw-r--r-- 1 www-data www-data  10015 Apr  5 19:16 rtl.css
-rw-r--r-- 1 www-data www-data 500231 Apr  5 19:16 screenshot.png
-rw-r--r-- 1 www-data www-data    744 Apr  5 19:16 searchform.php
-rw-r--r-- 1 www-data www-data   1904 Apr  5 19:16 search.php
-rw-r--r-- 1 www-data www-data    431 Apr  5 19:16 sidebar.php
-rw-r--r-- 1 www-data www-data   1223 Apr  5 19:16 single.php
-rw-r--r-- 1 www-data www-data  76750 Apr 15 10:42 style.css
drwxr-xr-x 2 www-data www-data   4096 Apr  5 19:16 template-parts

 

Next, open the functions.php file and add the code snippet at the bottom. Below shows an example using vim in linux to add the code snippet.

$ vim functions.php
...
/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer.php';

//Snippet for sorting posts by Modified Date
function order_posts_by_mod_date($orderby) {
        if  (is_home() || is_archive() || is_feed()) {
                $orderby = "post_modified_gmt DESC";
        }

        return $orderby;
}
add_filter('posts_orderby', 'order_posts_by_mod_date', 999);

 

Save the changes made to the functions.php file and reload your WordPress site. The posts will now be ordered by the date of modification rather than date of creation.

Leave a Reply

Your email address will not be published. Required fields are marked *