manski's blog

Sort posts by modification date in WordPress

By default, WordPress sorts blog posts by creation date. However, if you update your blog posts from time to time, you may want to sort them by modification date rather than creation date.

To achieve this, use this snippet:

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);

In your theme, just dump this snippet into functions.php. (You may need to create this file in your theme’s directory.)

11 comments

  1. tomas said:

    thanks a lot, it works..

  2. George said:

    It works great, Ive been looking EVERYTHING on the internet for the answer, after 50 different results, nothing. Thank you so much for this!

  3. Quy P. said:

    Thank you so much. The code works really well on my wordpress site. I also replaced the creation date -> modified date in the entry-meta.

  4. Hong Hanh said:

    Thank you so much. This saved me hours and hours of searching. It works well on my site.

  5. Pingback: WordPress Sort Posts by Modification Date -

  6. Ahmed said:

    Cool!

Leave a Reply to Ahmed Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.