# Order Blog and Archives by Last Updated

> Lists posts on the blog page and archives by the date they were last updated instead of the date they were published, so refreshed articles move back to the top.

- Type: PHP
- Runs: Run everywhere
- Category: [Archives](https://wpide.com/library/category/archive/)
- Tags: order, archives, modified-date
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/order-posts-by-last-modified/

## Code

```php
add_action( 'pre_get_posts', function ( $query ) {
    if ( is_admin() || ! $query->is_main_query() || ! ( $query->is_home() || $query->is_archive() ) || $query->get( 'orderby' ) ) {
        return;
    }

    $query->set( 'orderby', 'modified' );
    $query->set( 'order', 'DESC' );
} );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Order Blog and Archives by Last Updated" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
