Tag: last updated

Sort posts by last updated date in WordPress

Sort posts by last updated date in WordPress

Solution

In recent post list, the date of sorting is based on published date, so the updates will never be shown. To change this behavior, following code can be appended into functions.php to archive the sorting by last updated date.

function lmt_orderby_modified_posts( $query ) {
    if( $query->is_main_query() && !is_admin() ) {
        if ( $query->is_home() || $query->is_category() || $query->is_tag() ) {
            $query->set( 'orderby', 'modified' );
            $query->set( 'order', 'desc' );
        }
    }
}
add_action( 'pre_get_posts', 'lmt_orderby_modified_posts' );

Issue

There is an issue after changed sorting and modified post day, the archives will still be under published date, which can be comfusing.

If there is a way change permalink structure based on last updated date, then could be much simple.

References

Sort posts by modified date in the frontend
Using Permalinks
Simple Guide to Changing Your Permalinks Without Breaking Your WordPress Site