WordPress: How to Sort Category Archive Posts by Subcategory

By default, WordPress sorts posts in reverse chronological order, with the most recent post being at the top and the least recent at the bottom. Normally, that’s totally cool, since that’s the way we’ve been accustomed to reading blogs, but what about when you’d like a little more organization?

Let’s say, for example, we’ve got a Music category with music genres as subcategories and posts specific to those genres organized respectively:

  • Music
    • Rock
      • “The History of AC/DC”
    • Hip Hop
      • “What Ever Happened to Bell Biv Devoe?”
    • Pop
      • etc
    • Jazz
    • etc

When viewing the Music category archive, these subcategory posts will be displayed all mixed in together and in reverse chronological. What we’d rather have is each subcategory given its own individual listing of its posts, prefaced by an H2 title and subcategory/genre description. We’ll order the subcats alphabetically; their posts chronologically.

To do this, we’ll modify/create a category template for our Music category, in my case, category-17.php.

We’ll use the get_categories() WordPress function to create an array of the current category’s subcategories.

[php]child_of parameter tells the function to grab all the child categories of 17, the ID for our Music category. You’ll substitute whatever ID your category is. The function will automatically order the subcats alphabetically, but you can also set it to order by ID.

Next, we’ll use PHP’s foreach construct to iterate through each array asset, find its subcat ID, and query_posts() to grab that subcat’s posts.

[php]foreach ($categories as $cat) {
query_posts(“cat=$cat->cat_ID&showposts=-1&order=ASC&orderby=name”);[/php]

The showposts=-1 parameter tells query_posts to grab all the posts without stopping at the default number for your installation, and order=ASC orders them in normal chronological order (default is DESC, descending).

Then we use a simple WordPress loop to display that post’s goodies, close the foreach(), and let it go on to the next subcat.

Finished code:

[php]

cat_ID&showposts=-1&order=ASC&orderby=name”); ?>

cat_ID); ?>


[/php]

Explanation:

We grab an array of the category’s children, iterate through each one, make a new “genre_subcat” DIV, query the subcat, display a subcat header and description, loop through the posts with a while (have_posts()) construct, and finally display each post in a DIV with an H3 and some metadata, all before closing the while and querying the next subcategory in the array.

Of course, this is sort of a skeleton of what you could do, but you get the idea. Yay! Sorting a category’s posts by subcategory! Yayy!!

45 Responses to WordPress: How to Sort Category Archive Posts by Subcategory

  1. Pingback: 10 Awesome WordPress hacks to enhance your blog’s usability | Web Design & Development Blog

  2. Pingback: Plasterdog – Wordpress Instructions » Clear navigation from categeory to sub-category

  3. Pingback: 10 Enhancements for WordPress

  4. Pingback: The Best Of WordPress Snippets - Part 3 - JustWP.org

  5. Pingback: The BestOf WordPress Snippets Part – 5 - JustWP.org

Leave a Reply

Your email address will not be published.