- This topic has 5 replies, 2 voices, and was last updated 2 years, 12 months ago by Elvin.
-
AuthorPosts
-
May 4, 2021 at 1:13 pm #1764442Steve
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!
May 4, 2021 at 3:14 pm #1764516ElvinStaffCustomer SupportHi there,
Set the orderby parameter to ‘date’ instead of ‘modified’ so it only checks the post date.
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
May 4, 2021 at 8:40 pm #1764750SteveHi,
But where in the settings/code do I actually do that?
May 4, 2021 at 11:03 pm #1764866ElvinStaffCustomer SupportBut 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'); } });
May 5, 2021 at 3:47 am #1765321SteveHey 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?
May 5, 2021 at 6:14 pm #1766511ElvinStaffCustomer SupportI 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
-
AuthorPosts
- You must be logged in to reply to this topic.