Categories
WordPress

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.

By Kongo

Kongo enjoys sharing great and easy to implement tips on popular software and web technologies.

0 0 vote
Article Rating
Subscribe
Notify of
guest
17 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Evelyn Jeanne Shaw
Evelyn Jeanne Shaw
7 years ago

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.

Jonathan
Jonathan
7 years ago

Thanks. You saved me a lot of time!

Mike
7 years ago

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

Andrew
7 years ago

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

parham
parham
3 years ago
Reply to  Andrew

Hi
I tried this but does not work

style-bites
style-bites
7 years ago

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

Robert Seigler
Robert Seigler
6 years ago

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

cva
cva
5 years ago
Reply to  Kongo

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

sonali
sonali
3 years ago

good

Peter Olayinka
Peter Olayinka
3 years ago

Thanks alot for sharing.