Reverse post order

A simple solution to reverse post order in WordPress!

By default, WordPress shows your posts in order of most recent to oldest. Usually I’d rather show older posts first and tack the newest ones on at the bottom.

After searching for quite some time through WordPress.org and Google, I finally stumbled upon the key piece of code.

Inside of your theme, open your index.php file and look for this bit of code:

<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>

Right before that line, add this code:

<?php query_posts($query_string . "&order=ASC") ?>

That’s it, that’s all there is too it! Code is so lovely when you find the right one for your purposes… Just remember that this will only work for the theme you edit, and when you upgrade your theme, it will be lost.

32 Comments

  1. Stu says:

    It seems to only work if you make the quotes single ie: '&order=ASC'

  2. WebElaine says:

    Actually, it depends on your server configuration. On my server, it works fine with double quotes: “&order=ASC”

  3. themover says:

    Stu thanks very much for this tip, i was checking wordpress.org for a while now and didn't get this out.

    This is not depending on the server it is just a copy and paste failure. If you copy the line above and paste it in your code you're getting no "normal" double quotes. Sorry no idea how to call them.

  4. WebElaine says:

    It looks like the problem was this blog using "smart quotes" (slanted quotes) by default. I have updated the code with normal quotes so you can now copy and paste without having to edit anything.

  5. hermit says:

    Hi - I too would like to put my posts in 'natural' order instead of the bizarre, back-tofront style which is defualt; however, my index.php only says this:

    So, where does the code go??

    Congratulations on your new arrival!!

    Great site, love the name.

    Best wishes,

    Jim

  6. hermit says:

    Hey, what happened to my code??

    /**
    * Front to the WordPress application. This file doesn't do anything, but loads
    * wp-blog-header.php which does and tells WordPress to load the theme.
    *
    * @package WordPress
    */

    /**
    * Tells WordPress to load the WordPress theme and output it.
    *
    * @var bool
    */
    define('WP_USE_THEMES', true);

    /** Loads the WordPress Environment and Template */
    require('./wp-blog-header.php');

    • WebElaine says:

      Hermit, looks like you are using a template with nonstandard coding. I can't tell from what you posted where you need to put this code to change the post order; it might be in a different file. If you can post all the code from your index.php file here I'll see if I can figure out where you need to look.

  7. I used this code to do the exact opposite... arrange entries from most recent to least. PHP scares me. Thanks for the tip

  8. [...] Reversing the post order in WordPress Hi, Kim. I haven't tried it, but according to this post, here's the scoop: [...]

  9. sweatersbuy. says:

    Good article. I’ll definitely try to incorporate this into my own blog.

  10. dyno says:

    Thank you!

    This was fantastic... I was looking for a way to display posts in natural date order for printing and didn't find any. After ur tip, I added "&order=ASC" to the end of an URL did the trick.

  11. chris says:

    Works a treat, thanks for posting this. Needed to find the index.php of my theme used (wp-content/themes)

  12. Casandra says:

    Thanks soooo much...fast easy and not techno geek speak.

    Blessings!

  13. Doug says:

    Thanks!

    I also found this helpful tip on the WordPress site.

    ---
    ...to set the display order of the posts without affecting the rest of the query string, you could place the following before The Loop:

    global $query_string;
    query_posts($query_string . "&order=ASC");
    ---

  14. Rob S. says:

    I have been trying to get this code to work with the new twenty ten default theme for WordPress 3.0 and can't for the life of me figure it out.

    It does what it is supposed to on the first page of posts but it seems to be looping the older post and newer post links back to the first page of posts.

    They have added some new functions to the loop for the twenty ten theme so I am wondering if some of those functions are causing the problem.

    • WebElaine says:

      They did make some major adjustments in WP 3.0, and this particular code was designed to be used in version 2.x. Did you try Doug's suggestion from the WP site?


      …to set the display order of the posts without affecting the rest of the query string, you could place the following before The Loop:

      global $query_string;
      query_posts($query_string . “&order=ASC”);

  15. Erroline says:

    Thank you. I've been searching for this information on and off for months. It works. Much appreciated. Like everybody else, really liking the name.

  16. Laurie says:

    Hi, this bit of code is very sweet to find, but it doesn't seem to work on the posts I imported from the wordpress.com blog into the wordpress self hosted environment. It's fine going backwards from now to import date but then everything reverses. What I want is ALL posts on the home page to be in DSC order. I also can't get the front page of the site to only display the last 5 posts, which would also solve the problem, since everything else is correctly filed in archives.

    From my sleuthing I've found that the import brought in a blank field in the date published table in the php_myadmin area, so the only thing I can think of is to manually go into that part of the database and replicate each publication date into the date_gmt field beside it to force it into correct order. Seems awfully time-consuming and tricky.

    Here's my index.php code:

    <?php
    if(is_home() && function_exists('get_a_post')){//detect if FCG is active
    include (ABSPATH . '/wp-content/plugins/featured-content-gallery/gallery.php');
    }

    if (have_posts()) :
    $post = $posts[0]; // Hack. Set $post so that the_date() works.
    if(is_category()){
    echo 'Archive for the Category »'.single_cat_title('',FALSE).' «';
    }elseif(is_day()){
    echo 'Archive for » '.get_the_time('F jS, Y').'«';
    }elseif(is_month()){
    echo 'Archive for » '.get_the_time('F, Y').' «';
    }elseif(is_year()){
    echo 'Archive for » '.get_the_time('Y').' «';
    } elseif(is_search()){
    echo 'Search Results';
    }elseif(is_author()){
    echo 'Author Archive';
    }elseif(is_tag()){
    echo 'Tag-Archive for » '.single_tag_title('',FALSE).' « ';
    }elseif((is_home()||is_front_page()) && $paged>1){ // If this is a paged archive
    echo 'Blog Archives';
    }else{
    echo ' ';
    }
    while (have_posts()) : the_post(); ?>
    <div id="post-" >

    <a href="" rel="bookmark" title="Permanent Link to ">

    -
    ID,$tpinfo['templatelite_postage_postthumb_width'],$tpinfo['templatelite_postage_postthumb_height'],'img','post_thumb');?>

    Category:
    <?php the_tags(' Tags: ', ', ', ''); ?>
     

    max_num_pages > 1):
    ?>

    Not found
    "Sorry, but you are looking for something that isn't here. Try something else.

  17. Jacob says:

    Hey!

    Just tried this on a website I'm working on for a client of mine, and it worked perfectly. Thank you thank you.

    -J

  18. aberling says:

    Worked like a charm for me (in WP 3.01). Thanks for the simple fix!

    Good luck to you all with other problems.

    Best, A

  19. Ant says:

    Thanks for the hint

    That's great if you want to create, for example, a long tutorial that people are meant to follow from the first post on

  20. buy kinect says:

    Hi,

    I have a question for the webmaster/admin here. Can I use part of the information from your blog post above if I give a backlink back to your website?

    Thanks,
    Jules

  21. Thanks for a well documented solution to this issue. Worked on the first try.

  22. Bob Baum says:

    Want to reverse order, and need your help. I'm new to blogging and php. Hosted with Godaddy. Using wp 3.1. Went to "theme" file. Index is only about three lines.

  23. 3ICE says:

    A less obtrusive solution would be to edit the wp-includes\query.php file:
    Change
    $q['order'] = 'DESC';
    to
    $q['order'] = 'ASC';
    (line 2290 by default)

    Your method also breaks categories. (All posts are listed, not just the ones in the category.) My method works with categories and tags.

    • WebElaine says:

      Thanks for the alternate option. My method does not break categories unless you have a theme with no category template file (because WordPress has to use the default index.php in that case.) I prefer to edit the theme, not WordPress core files which may be overwritten the next time I upgrade WordPress. Of course, now that themes have versions, you have to make my edit in a child theme or it will break next time you update the theme.

Leave a Reply