Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have my posts ordered ascending (from oldest to newest) because I use Wordpress as a website to accompany a book published based on the original blog. To do this I put the following code just before The Loop in index.php of my custom version of the Connections theme:

<?php
    $wp_query->set('orderby', 'post_date');
    $wp_query->set('order', 'ASC');
    $wp_query->get_posts();
?>

This works fine for the main index and for when the posts are displayed by page, i.e.:
index.php?paged=1

However, when the posts are retrieved by month or by category, the sort order defaults to DESC, i.e.: http://www.justanothersoldier.com/?cat=4

http://www.justanothersoldier.com/?m=200312

I just want everything to always be ASC. Are there monthly and category templates I need to edit? Thanks.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

You can hook into the posts_clauses filter and change ASC to DESC there with a simply str_replace. The filter has one argument $pieces, which is an array of the MySQL statement parts.

share|improve this answer

In some cases it might be good to know that you can just add parameters to the url, like

http://www.justanothersoldier.com/?cat=4&order=asc

which is sufficient if you use menu entries to access categories, each with their specific sort order defined by such an url parameter.

(How to associate certain categories with individual 'per-category-defined' sort order, so that it'll work even when selecting a category by clicking on the posts' category label - in which case the required url parameter gets lost again - is another question. I've posted that here: How to choose a sort order (for posts) per category? (ideally when creating a new category) - 1st answer again suggests to alter some code.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.