• Hello,

    On my wordpress site, the reading is display latest post first and I want to change it a bit by display “latest modified post” first.

    Could someone recommend a way to do the trick please?

    Thank you for your help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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.

    Thread Starter longnha

    (@longnha)

    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 ??

    Thread Starter longnha

    (@longnha)

    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

    Thread Starter longnha

    (@longnha)

    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 8 years, 3 months ago by longnha.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Reading: Latest Modified Posts First’ is closed to new replies.