2

I've read the codex article about WP_Query's orderby parameters and I understood that it is possible to order by Modified date. But I couldn't find where should I edit.

How WordPress post order by modified date including home page as well as categories page and tag pages?

1
  • With everything I explained in the duplicate, and with the use of the appropriate conditionals, it should work exactly the same, no difference Jul 15, 2014 at 12:25

3 Answers 3

8

Try this:

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

This will set orderby field to modified in the WordPress query.

1
  • Doesn't work for me, with GeneratePress.
    – Shawn
    Mar 7, 2019 at 8:57
2
function orderby_modified_posts( $query ) {
    if($query->is_main_query()) {
            $query->set( 'orderby', 'modified' );
       }
}
add_action( 'pre_get_posts', 'orderby_modified_posts' );

This should alter all archives.

1
<?php
function modified( $query )
{
    if ( $query->is_main_query() && ( $query->is_home() || $query->is_search() || $query->is_archive() )  )
    {
        $query->set( 'orderby', 'modified' );
        $query->set( 'order', 'desc' );
    }
}
add_action( 'pre_get_posts', 'modified' );

?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.