Hi @longnha,
Place below code in your active theme’s function.php and you will get what you want.
add_action( ‘pre_get_posts’, ‘alter_query_to_show_modified_post_first’ );
function alter_query_to_show_modified_post_first( $query ) {
if( !is_admin() && $query->is_main_query() ) {
$query->set( ‘orderby’, ‘modified’ );
}
}
Looking forward to hear from you.
Hi @simplerthansimplest
Wonderful, it works. Thank you very much.
A bit need to change for anyone who need it.
add_action( 'pre_get_posts', 'alter_query_to_show_modified_post_first' );
function alter_query_to_show_modified_post_first( $query ) {
if($query->is_main_query() ) {
$query->set( 'orderby', 'modified' );
}
}
Good to hear that it worked.
Removing admin condition will list recently modified post first both on admin side and frontend. If it is that you wanted then its COOL 🙂
My apologize, I removed !is_admin it to test and forgot to add it back. Updated code:
add_action( 'pre_get_posts', 'alter_query_to_show_modified_post_first' );
function alter_query_to_show_modified_post_first( $query ) {
if(!is_admin() && $query->is_main_query() ) {
$query->set( 'orderby', 'modified' );
}
}
Thank @simplerthansimplest
Seem like there have a conflict! I’ve many scheduled posts, when a scheduled post is published, it no longer display at top because the code check ‘modified date’ of scheduled post, not ‘publish date’
Is there a way for ‘latest modified’ and ‘latest post’ to work together?
Thank you.
-
This reply was modified 9 years, 5 months ago by
longnha.