2

I have a custom taxonomy template, the items are being displayed in the default order, from newest to oldest post.

How would I change the custom taxonomy template to list the posts in the opposite order? Is there a function for this?

2
  • Can you share the part of the code that's displaying the posts? For most kinds of queries, you can set 'order' => 'DESC' and that will put them in descending (reverse) order.
    – WebElaine
    Commented Oct 2, 2017 at 19:56
  • It's on a taxonomy template file, so the loop is just the standard.. "while ( have_posts() ) : the_post();" type of loop.
    – Richard
    Commented Oct 2, 2017 at 20:36

1 Answer 1

5

You can do this with the pre_get_posts action. pre_get_posts fires after the query variable object is created, but before the actual query is run. So you won't suffer performance penalties by running multiple unnecessary queries.

In your functions.php:

add_action('pre_get_posts','xx_taxnomy_query');
function xx_taxnomy_query($query) {
    if ($query->is_main_query() && ! is_admin() && $query->is_tax('your_taxonomy')) {
        $query->set('order', 'asc');
        return;
    }
}

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.