Change the order of posts in an archive page

WordPress displays posts according to publish date, newer comes first. It happens in all archive pages, even in home page.

Did you ever need of changing order of posts in archive page? Or you thought? Well, there is an easy code snippet to shows posts in reverse order. You can add this codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution.

[php]
function change_order($orderby, $query) {
global $wpdb;
if(is_archive())
$orderby = “{$wpdb->prefix}posts.post_date ASC”;
return $orderby;
}
add_filter(‘posts_orderby’,’change_order’);
[/php]

Here is_archive() is used to check if the page is only an archive. If you want for all pages, just remove that conditions.

Also if you need in home page, tag archive page or even in search results page you can use the relevant conditions like:
[php]
is_home()
is_tag()
is_search()
[/php]

Enjoy!

Leave a Reply

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