0

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.

2 Answers 2

0

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.

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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