Sort posts by Date (DESC) and by Title (ASC)

You can pass an array to the query as the following example described in the Codex shows:

$args = array(
'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' )
);

$query = new WP_Query( $args );

In your case will be something like this:

/* Order Posts Alphabetically */
function prefix_modify_query_order( $query ) {
  if ( is_main_query() ) {

    $args =  array( 'post_date' => 'DESC', 'title' => 'ASC' );

    $query->set( 'orderby', $args );
  }
}
add_action( 'pre_get_posts', 'prefix_modify_query_order' );

If you want the post_date as the primary filter, you have to change his position in the array, now the code will query all the posts alphabetically starting by the newest post_date.

Leave a Comment