Archived topic

How to order posts by publication date rather than last updated?

5 replies · Started by Victoria on May 4, 2021

Viewing posts 1–6 of 6

Currently, the posts on our posts page are listed according to the date they were last updated, but this means that an old post which is updated gets bumped up to the top of the lists again. This is not ideal behaviour, so we want the order to always be the original publication date.

I understand that I need to insert an 'orderby' parameter somewhere into where the loop is being generated, but not sure how to actually go about doing this.

Thanks!

Hi,

But where in the settings/code do I actually do that?

But where in the settings/code do I actually do that?

It should be the default for the blog page and the archive pages of the theme. Are you pertaining to these?

But this can change if you have custom templates for archive pages or blog index page.

Or if you use pre_get_posts on one of your PHP snippet.

Example:

add_action( 'pre_get_posts', function(){
if ( ! is_admin() && is_home() && $query->is_main_query() ) {
        $query->set( 'orderby', 'date' );
        $query->set( 'order', 'ASC');
   }
});

Hey Elvin,

I tried adding your code to my child theme's functions.php but it seems to break the blog roll page.

Our blog roll page is: https://bridgesandballoons.com/latest-stories/

This works fine without that code being added, but I get a 'critical error' when I add that code.

It should be the default for the blog page and the archive pages of the theme. Are you pertaining to these?

I don't understand this. Do you mean that it should already be sorting by publication date?

I tried adding your code to my child theme’s functions.php but it seems to break the blog roll page.

Our blog roll page is: https://bridgesandballoons.com/latest-stories/

My bad. sorry I forgot to pass the $query variable to the function.

Try this instead:

add_action( 'pre_get_posts', function($query){
if ( !is_admin() && $query->is_home() && $query->is_main_query() ) {
        $query->set( 'orderby', 'date' );
        $query->set( 'order', 'DESC');
   }
});

I don’t understand this. Do you mean that it should already be sorting by publication date?

Yeah. The default sorting order is orderby = 'date' (post_date) and order = 'DESC' or descending meaning, newest first. Newest to oldest.

Check default values - https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

This archived topic is closed to new replies.