How to Change Latest Posts Order By Last Modified Date

In this article we will learn how we can change the order of latest posts. Usually newly published posts appears upper then any other posts, but by using the following code you can display modified posts upper then new published posts.

To display the post order by modified date in wordpress.

You have to copy and paste the following code into your theme’s functions.php file. You can go there from hosting access and you can also edit that from wordpress admin panel. Just go to appearence then theme editor. then you will see list of theme files, just click on functions.php an copy the following code and paste it just below “<?php”.

How to Change Latest Posts Order By Last Modified

function wpb_custom_query( $query ) {

// Make sure we only modify the main query on the homepage 
if( $query->is_main_query() && ! is_admin() && $query->is_home() ) {

// Set parameters to modify the query
$query->set( 'orderby', 'modified' );
}
}

// Hook our custom query function to the pre_get_posts 
add_action( 'pre_get_posts', 'wpb_custom_query' );

If  you have any questions, you can ask in comments.

Leave a Reply

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