Reverse post order in WordPress

Note: When I first discovered this technique, WordPress themes were simpler. Reversing post order is a little trickier now. I'll try to cover the basics and the reasoning to increase your chances of success!

By default, WordPress shows your posts in order of most recent to oldest. But many people would rather show older posts first and tack the newest ones on at the bottom.

Changing the order of posts means you have to edit your theme. If you've never done this before, it's not impossible, but it can be tricky. I highly recommend reading Yoast's Anatomy of a WordPress Theme to get a basic idea of how themes work. I also highly recommend setting up a test blog - a completely separate installation of WordPress where you can install and edit themes. That way, if you change something and break the theme (which is easy to do), it won't take down your whole blog.

Quick and dirty Reverse Post Order

There's an easy way to reverse your post order. It's quick and dirty, but for those of you who want a fast solution, here it is:

    

Log in to your WordPress dashboard. Click Appearance > Editor.

    

Now, locate the place you want the post order reversed. If you want the order reversed on the homepage, click on the Main Index Template (index.php). If you want categories, look for Category Template (category.php) or possibly Archives (archive.php). Again, Yoast's Anatomy can give you more info on which file you're looking for.

    

Inside the template you've chosen, look for this line of code:

    

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

    

Right before that line, add this code:

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

That's the easy way. Just keep in mind, if you ever upgrade your theme, it will lose this - although it's rather easy to add it back in, probably easier than doing things the "right" way with a child theme. Keep reading if you want to see the clean way to reverse your post order.

Child themes

The problem is, if you're using a WP theme someone else developed, they may offer updates from time to time. So if you make little tweaks like reversing the post order, and then you update the theme, you'll lose all your tweaks. (And it's important to update frequently, because updates often include security fixes. That's right, themes can have security holes just like any other component of WordPress.)

In order to solve for this problem, WordPress introduced child themes. You install a theme (the main theme is considered the "parent theme") and then add a few files with your modifications. When you upgrade the parent theme, your modifications stay intact because they are in completely separate files that are overriding the parent theme. I personally don't like the parent/child theme approach because it seems like too much overhead - the parent theme tells WordPress to display things one way, then it gets overriden by the child theme, which means WordPress is processing everything twice. But, if you're not comfortable making significant edits to an existing theme, child themes can be very helpful.

How to reverse post order in Twenty Eleven

Since every theme is different, I'll explain how to reverse the post order on the homepage of the Twenty Eleven theme the "right" way, with a child theme. Please let me know in the comments if you have any questions or suggestions.

You will need FTP access to your blog, as well as some sort of text editor. I prefer Notepad++ and FileZilla, but plain old Notepad and a File Manager from your host will work.

Step 1: Install the Twenty Eleven theme if you don't have it already.

Step 2: Open your text editor and create a file called "style.css". Add the following info to this file:

/*
Theme Name:     Reverse Twenty Eleven
Description:    Reverse post order override
Template:       twentyeleven
*/
@import url("../twentyeleven/style.css");

This file is required, because it tells WordPress to override the Twenty Eleven theme. Now create a second file called "index.php". What we want to do is copy the original "index.php" from the Twenty Eleven theme, then add the one line of code that reverses things. I've copied the full index.php and also added the new line of code in red, so you can just copy and paste this whole chunk of code into your new "index.php" file:

<?php
get_header(); ?>
        <div id="primary">
            <div id="content" role="main">
            <?php if ( have_posts() ) : ?>
                <?php twentyeleven_content_nav( 'nav-above' ); ?>
                <?php /* Start the Loop */ ?>
                <?php global $query_string; query_posts($query_string . "&order=ASC"); ?>
                <?php while ( have_posts() ) : the_post(); ?>
                    <?php get_template_part( 'content', get_post_format() ); ?>
                <?php endwhile; ?>
                <?php twentyeleven_content_nav( 'nav-below' ); ?>
            <?php else : ?>
                <article id="post-0" class="post no-results not-found">
                    <header class="entry-header">
                        <h1 class="entry-title"><?php _e( 'Nothing Found', 'twentyeleven' ); ?></h1>
                    </header><!-- .entry-header -->
                    <div class="entry-content">
                        <p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
                        <?php get_search_form(); ?>
                    </div><!-- .entry-content -->
                </article><!-- #post-0 -->
            <?php endif; ?>
            </div><!-- #content -->
        </div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Finally, you'll also want to change the default next/previous navigation, as Twenty Eleven is set to say "Older Posts" (which would now actually point to newer posts) and "Newer Posts" (which would now actually point to older posts). I suggest changing this to Next and Previous links, because these links will show up in several places, including the homepage and categories. If you only reverse post order in one place and leave the others the same, then some "older" links will be accurate and others will not. If you add "Next" and "Previous" links, it doesn't matter whether next is older or newer - it should always make sense to readers.

Create a file called "functions.php" with the following content:

<?php function twentyeleven_content_nav( $nav_id ) {
    global $wp_query;
    if ( $wp_query->max_num_pages > 1 ) : ?>
        <nav id="<?php echo $nav_id; ?>">
                <div class="nav-previous"><?php previous_posts_link( __( '<span class="meta-nav"> &larr; </span>', 'twentyeleven' ) ); ?> More Posts <?php next_posts_link( __( '<span class="meta-nav"> &rarr; </span>', 'twentyeleven' ) ); ?></div>
        </nav><!-- #nav-above -->
<?php endif; } ?>

In your FTP client or File Manager, go to this directory:

wp-content/themes

Create a new subdirectory called "reverse2011" and upload your three new files into the "reverse2011" directory.

Last step: go to your WordPress dashboard, choose Appearance > Themes, and find your "Reverse Twenty Eleven." Click Activate, and voilà, your Twenty Eleven homepage posts are in reverse order.

If you'd like to reverse the order on categories too, just copy Twenty Eleven's original "category.php" file, paste it into your own new file, and copy & paste that red line of code right above the line that says <?php while ( have_posts() ) : the_post(); ?>.

62 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.

    • @WebElaine says:

      For the front page post limit, there's a Dashboard setting. Go to Settings > Reading and set "Blog pages show at most" 5 (or whatever max you want to set).

      For the order, in the code you posted, you'd need to add my one line of red code

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

      directly above this line in your index.php:
      while (have_posts()) : the_post(); ?>

  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.

  24. Kevin says:

    Thanks for the help! I've just made my posts show up in reverse order using your tip, but I'm having a problem. I want wordpress to only show the latest 3 entries, with the newest one at the bottom. Instead, it shows the first three entries and I have to click next to see the newer posts. I'd like my default page to be in reverse order but showing only the 3 most recent posts. Any idea?

    • @WebElaine says:

      Hi Kevin,

      You would want to change this line of code:

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

      to this:

      <?php global $query_string; query_posts($query_string . "&order=ASC&posts_per_page=3"); ?>

      That should force WP to only look up the 3 most recent posts, then put them in reverse order.

  25. MJ says:

    You instructions and code seem pretty easy but I am stuck at this step:
    Inside of your theme, open your index.php file and look for this bit of code:

    How exactly do I find the INDEX.PHP file because I am having no luck.

    • @WebElaine says:

      MJ - on your FTP server, in the folder where you have WordPress installed (often "/public_html/"), go to "wp-content/themes/". There will be a subfolder for each theme, so go into your theme's subfolder and that's where you'll find e index.php file you need to edit.

  26. Mark says:

    Trying to get this to work. I'm pretty sure I copied everything exactly, but when I activate the reverse theme, it really hoses the page. Takes away the formatting/layout, makes it look like a web page from 95. I tried copying the entire 2011 theme into the reverse2011 directory, then editing its files, but got pretty much the same problem, except it doesn't like the changes to the functions.php file, says syntax error...

    • @WebElaine says:

      Hi Mark - my most sincere apologies, it looks like my original code didn't copy over so well. Please try again using the freshly updated code in these boxes - you probably won't notice much difference, but a question mark and a semicolon are likely the culprits. I've retested with the new code here and it should work - please let me know if this helps.

  27. Mark says:

    Ok thanks, I tried again. Same thing, I'll leave it alone for a bit, If you could take a look, I'd appreciate it. It's moved the stuff on the right hand edge to the bottom of the page, I think the font is different, too, not sure.

    Any relation to Ross Shannon over at yourhtmlsource.com?

    • @WebElaine says:

      Mark, thanks for leaving it up so I could see what was happening. So sorry, I had one last line of code missing, the one that imports the Twenty Eleven stylesheet. In "style.css" add this line at the bottom:

      @import url("../twentyeleven/style.css");

      (also updated in the main article above.) I appreciate your patience and hope this works for you! I don't think I'm related to Ross - at least, not very closely. :)

  28. Mark says:

    MUCH better! Thank you so very much!

  29. Gus says:

    Hi,
    Every month I need to -just once- change the default order of my blog. I can then print out this months worth of posts in the old>new order. Until recently this worked fine by using:
    http://haikustinus.wordpress.com/?orderby=date&order=ASC
    Now this only gives me the first week of my blog (january 2009) and then it skips again to March 2012. ???
    Tried to follow the quick and dirty instructions you mentioned (am quite digilliterate I'm afraid) but in my 'appearance' I can find no 'editor'. Any suggestions? TIA Gus

  30. Gus says:

    Hi Elaine,
    Thanks for replying. Kinda figured that was the reason why I didn't get an Editor ;o\
    Thanks also for the tip but sad to report that it doesn't seem to work. The page that opens after clicking yr link just has:

    "Archief voor maart, 2012
    op 1 maart 2012"

    or (in English)
    "Archive for March, 2012
    on March,1 2012"

    The March 1 date is a link to the post of March 1.
    So no whole month is being shown and the posts itself is not being shown.

    Maybe you have an additional tip?

    TIA
    Guus

    • @WebElaine says:

      Hi Guus (sorry if I spelled your name wrong - I saw one U on your last comment. :) -

      It looks like monthly archives are somehow not enabled on your blog. On self-hosted blogs, this can be caused by SEO plugins that disable archives - if there are any SEO / archive settings you might want to check those.

      As a workaround, you could try creating a temporary post using the Archives Shortcode - [archives type=monthly order=asc] - just paste that piece of code, including the [ ] brackets, into a new post. Once you publish the post, it will then give you the list of posts you're looking for. After that point you could either leave it live or delete it. This is kind of a quick and dirty solution, but I figured I would offer it in case it helps.

  31. guus says:

    Hi Elaine,
    Thanks again for yr help. Unfortunately it is another dead end. Posting the AScode results in a page with all the months (in ascending order: good)but klicking on i.e. March 2012 leads to a page with only 1 line/link that refers in its turn to the post of March 31 (and only thát day).
    I've found a workaround by pasting the actual posts (in descending order ;o( in a Word doc and then juggling it around using Tables(don't ask..;o)
    We'll hope for better days.
    Thanks again for your help, Elaine.
    Guus (=Dutch for Gus)

  32. Mike s says:

    You're missing a ? after the opening bracket, before the php. It should read:

    Mike.

  33. Luci says:

    Thank you for your code. It seems to be working on a Twentten child theme on a category template with WP 3.3.2. Now I'd like to be able to rearrange the posts by name.

  34. Tansel says:

    Thank you very much, reverse post order worked easily...

  35. Henk says:

    Hi,

    Firstly, thanks very much for an awesome guide. I've used this for the 2011 theme, and now want to use it for the 2012 theme with WP 3.5

    I've adapted the guide and it seems to be working, but somehow I've now messed up the style of the site.

    I've uploaded a screenshot of the new look of the site (lol), as I'll switch back to the standard 2012 theme for now. The post order is reversed, so the problem is just the style that's messed up.

    http://cl.ly/LXMU

    My guess is that it's something to do with the fact that the functions.php (as I didn't change this, and just copied it, changing the name twentyeleven to twentytwelve (which I don't think will cut it). I also uploaded my files, so that you can see what I did.

    http://cl.ly/LXDN

    There may also be a problem with the inserted line, since it seems that some of the lines doesn't have the closing php tags included, so adding them in the inserted line will obviously mess things around (I removed the tags in the inserted line in these cases)

    Can you help with this?

    Thanks if you can!

    • @WebElaine says:

      Hi Henk,

      You're quite welcome. Anything I can do to help people use WordPress benefits everyone - myself included. :)

      Did you create a child theme following these instructions? If so, you need to use the index.php from TwentyTwelve instead of the TwentyEleven one posted here. Just go to your TwentyTwelve theme, copy & paste index.php into a new index.php, and then follow the other child theme instructions here. In your index.php you'll just need to copy the line in red from my TwentyEleven index.php above. Let me know if all that makes sense.

      If you didn't create a child theme, please delete your copy of TwentyTwelve and reinstall the one from WordPress.org. Then follow the child theme instructions. Some themes can be hacked the easier way I explain in the beginning of this post, but TwentyTwelve is a complex theme and you should definitely be working with a child theme rather than the original theme files. I know it is kind of a pain, but once you get the hang of it it's not so bad, and you won't have to worry about keeping TwentyTwelve itself up to date.

  36. Frans Geraedts says:

    Is it possible to make it so that the posts with a certain tag appear in reversed order? If and when a reader clicks on the tag? While the blog stays in the normal order?

    • @WebElaine says:

      It sure is, but themes have several different ways of displaying tags, so I'm afraid I can't post exact code here. Basically you would find whatever template handles tags in your theme - it could be archive.php - and put in a PHP "if" statement that if WP is displaying a tag archive, it reverses the order. If you're using Twenty Ten or Twenty Eleven, reply back and I will find out exactly what code you need to place and where to put it.

Leave a Reply