• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2784

Original Genesis Tutorials & 6000+ Guaranteed Code

Snippets

  • Consultation
  • Full Access
  • Log in

Reverse Post Order For Category Archive

In this tutorial, i’ll provide the most efficient solution which enables you to change the order of posts in one or all category archives from ascending to descending.

A forum member asked this question:

I’m looking for a way to print one of my categories in ascending chronological order while leaving all others descending.

I’m only just starting out with PHP, so forgive any goofy errors. I found the following code somewhere online (the category in question is ID 8):

The solution they posted included query_posts which you should NOT use as its a highly inefficient way to modify a loop as explained by the lead Developer of WordPress Andrew Nacin some time ago.

Note: This function (query_posts) isn’t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of pre_get_posts hook, for this purpose. Source Codex

The Most Efficient Solution

Please copy ALL the code from the view raw link in the Gist and paste it at the end of your child themes functions.php file using a text editor like Notepad++.

The above code displays all posts in category-8 from oldest to newest meaning the oldest posts will be displays at the top if the first page in the category archive.

Simply change ASC to DESC to display posts on your category archives from latest to oldest.

Reader Interactions

Comments

  1. Patty says

    November 4, 2014 at 8:17 pm

    Thank you so much. This is exactly what I needed and it worked perfectly. Really appreciate you posting it.

    Log in to Reply
  2. Douglas Barnes says

    October 8, 2014 at 7:44 pm

    Cracked that nut. Thanks for you consideration and this code snippet in any event!

    Log in to Reply
    • Brad Dalton says

      October 9, 2014 at 2:12 am

      Good stuff Doug!

      Log in to Reply
  3. Ian says

    July 17, 2014 at 8:52 pm

    Perfect solution for an events category page using future-dated posts. Thanks.

    Log in to Reply
  4. nel says

    July 1, 2014 at 11:46 pm

    Oh great! This is what I need for.

    Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags