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.
Leave a Reply