Celebrating 7 Years! Get up to $30 off GeneratePress Premium!

[Support request] How to order posts by publication date rather than last updated?

Home Forums Support [Support request] How to order posts by publication date rather than last updated?

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

  • This topic has 5 replies, 2 voices, and was last updated 1 week ago by Elvin.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1764442
    Steve

    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!

    #1764516
    Elvin
    Staff
    Customer Support

    Hi 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

    A wise man once said:
    "Have you cleared your cache?"

    #1764750
    Steve

    Hi,

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

    #1764866
    Elvin
    Staff
    Customer Support

    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');
       }
    });

    A wise man once said:
    "Have you cleared your cache?"

    #1765321
    Steve

    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?

    #1766511
    Elvin
    Staff
    Customer Support

    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

    A wise man once said:
    "Have you cleared your cache?"

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.