Change WordPress Post Order from Descending to Ascending


Update 12/2015: I’m no longer updating this post so I can’t guarantee compatibility with latest version of WordPress. Test on your Localhost first before implementing it on your live site!

Do you wish to change the order by which your WordPress posts or post excerpts are displayed? You’re not alone! If you write your posts in a chronological manner, you do want to change the default WordPress post order from descending (new to old) to ascending (old to new).  How do you achieve this?

By the way if you’re looking for a really good web host, try Bluehost – I found the setup procedure and their control panel very easy to adapt to and their IT team is awesome!

Changing WordPress Post Order

Method 1: Use a Plugin.

There are a lot of plugins available that enable you to change WordPress  post order. I tried a few of them and was genuinely impressed with their functionality and ease of use. Search for “post order plugin” in the WordPress plugin directory and choose one that fits your needs. I strongly recommend that you use a plugin to set your custom post order; unless you don’t like plugins!

A quick word on plugins: rarely do I recommend a specific plugin. Mainly because a new version of WordPress is available every few months, and it’s wise to update your WordPress installation. However if a plugin is not updated regularly, and most free plugins are not, there is a possibility it will not work with the new wordpress installations. Generally I find that plugins slow down my site and they make it very hard to optimize my code. That’s my two cents of plugins.

Method 2: Edit your template’s functions.php

This requires very little knowledge of php or html. It’s easy as copy and paste.  If you already know how to access your functions.php, skip to Step 2.

Step 1: Find the functions.php

There are two ways to can access your template’s function.php: WP dashboard or your cPanel. From your WordPress Dashboard, go to appearance-editor. From the right hand sidebar, choose the functions.php file.  Please note that you do not have access to the undo changes function and any saved changes are permanent!  From your cPanel, you can edit the functions.php through the file manager. In most cases you will find it in this folder path: /public_html/wp-content/themes/YOUR THEME/.  Most cPanel file managers allow you to search for the file, at least mine does!

Step 2: Edit functions.php

To quote from the WordPress Codex, “query_posts() is the easiest, but not preferred or most efficient, way to alter the default query that WordPress uses to display posts.” Why? Because it replaces the main query with a new query after the main query has run. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with pagination). WordPress recommends using pre_get_posts hook for changing the post order.

I’ll not share with you how to use query_post(), here is the a better way and easier way; it uses the  pre_get_post filter. Copy and paste the following code to your functions.php. Where? Anywhere generally works, but try and paste it just before the end, inside the ?>. The ?> marks the end of the code in functions.php, so you want to add your code just inside that. Here’s the code. You need to copy and paste everything from // to }

// Runs before the posts are fetched

add_filter( ‘pre_get_posts’ , ‘my_kongo_order’ );

// Function accepting current query

function my_kongo_order( $query ) {

// Check if the query is for home and alter only the primary query

if($query->is_home && $query->is_main_query())

// Query was for home, then set order

$query->set( ‘order’ , ‘asc’ );}

Step 3: Save functions.php

Save the functions.php file and check your blog for changes!

NB:  the above code only modifies the main query. For instance  the Recent Post widget orders the post in descending order!

Would you like more WordPress customization tips?  Leave a comment and let me know.


17 responses to “Change WordPress Post Order from Descending to Ascending”

  1. Evelyn Jeanne Shaw Avatar
    Evelyn Jeanne Shaw

    I couldn’t have asked for a better blog. You happen to be always at hand to offer excellent guidance, going straight away to the point for easy understanding of your site visitors. You’re really a terrific pro in this subject matter. Many thanks for being there visitors like me.

  2. Jonathan Avatar
    Jonathan

    Thanks. You saved me a lot of time!

    1. Kongo Avatar
  3. Mike Avatar

    I’m curious if there’s a way to only show posts in ascending order by using a query? I want my posts to remain in descending order, but have a link in the sidebar to “Read from the beginning.” Something like http://www.siochana.us/?order=asc

    Is that possible? Thanks!

    Mike

    1. Kongo Avatar

      If you wish to change the order of the “Recent Post Widget” you need to use the a conditional tag. is_dynamic_sidebar() should work, though I’ve not tested it. Let me know if that works.
      Kongo

  4. Andrew Avatar

    I was creating a post type for our church’s small groups and found this to be really handy to default the “smallgroup” type to sort by title on both the front and back end and also to have the admin recognize it is sorting by title:


    //set default front and back-end sort of custom post type
    add_filter( 'pre_get_posts' , 'my_nssg_order' ); // Runs before the posts are fetched
    function my_nssg_order( $query ) {
    // Check query and alter only the query needed
    //echo ''; print_r($query); echo '';
    if ($query->query['post_type'] == 'smallgroup' && !isset($query->query['orderby'])) {
    $query->set( 'orderby' , 'title' );
    $query->set( 'order' , 'asc' );
    //get the arrow to show up over title in admin
    if (is_admin()) {
    $_GET['orderby'] = 'title';
    $_GET['order'] = 'asc';
    }
    } //if
    } //my_nssg_order

    1. Kongo Avatar

      Great Share! Haven’t implemented it yet (can’t think of a practical use), but I’m sure sure I’ll find a use for it. Just to be clear, this will order a custom post type alphabetically, uses the titles. You can change the post_type to ‘post’ for your blog. Wonder if you could do use this (or a version of it) to order post by number of comments?
      Thanks Andrew.

    2. parham Avatar
      parham

      Hi
      I tried this but does not work

      1. Kongo Avatar

        Never got to try the code. so can’t say for sure.
        This post is over 5 years old!
        And wordpress is regularly updated, I’d be surprised if it still works…

  5. style-bites Avatar
    style-bites

    Hi! I have the Metrovibe theme and am trying to figure out how to use descending for my services slider on main page. help!

    1. Kongo Avatar

      Would love to, is the site live? Can you provide a link. Metro Vibe is a premium theme so I don’t have access to the code. Most probably the author is using the excerpt filter to do something else… but I’m clutching at straws here.

  6. Robert Seigler Avatar
    Robert Seigler

    Any reason why the nerds at Word Press cannot give us a simple “sort choice to asc or desc”/

    1. Kongo Avatar

      WordPress is a community and we are members of that community. There is no us and them! That’s the beauty of it. The solutions to our needs need to come from us. Adding the code about is simple enough. Or create a simple plugin to do this. It’s really not that hard and you’ll learn so much!
      All the best.
      And let me know if you (or anyone else for that matter) do create that plugin!

      1. cva Avatar
        cva

        i would like to know if it’s possible to sort the posts when i click Next or previous from my posts….example if am on Crime category and i click next i want to all posts in category but in ascending order……is it possible, i have banged my head with this now am having a headache

        1. Kongo Avatar

          You can sort posts and you can sort categories, so I am sure you can sort posts in categories… the WP Codex is my go to resource – it’s easier (and less painful) than banging your head against the wall.

  7. sonali Avatar
  8. Peter Olayinka Avatar
    Peter Olayinka

    Thanks alot for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *