Site logo

Archived topic

Sort by date on Category pages

1 reply · Started by Jonah on July 25, 2018

Viewing posts 1–2 of 2

I am trying to get category pages to sort by date, for some reason it still sorts by title. Is there a function I should override? This is the code I am using.


	add_action( 'pre_get_posts', 'my_change_sort_order'); 
    function my_change_sort_order($query){
		if($query->is_archive() || $query->is_category()):
			
			//If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type )
           //Set the order ASC or DESC
           $query->set( 'order', 'DESC' );
           //Set the orderby
		   $query->set( 'orderby', 'post_date' );
		   return;

        endif;    
    };

I think it should be: $query->set( 'orderby', 'date' );

Let me know :)

This archived topic is closed to new replies.