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

Damien Carbery - Website Development

Getting You Online & Keeping You There

Getting You Online & Keeping You There Get Started
  • Home
  • Services
  • Maintenance Plans
  • Portfolio
  • Blog
  • About Me
  • Contact
You are here: Home / Website Development / Change post order by category

Change post order by category

Published: August 31, 2020

Order a specific category by date, the rest alphabetically.

In WordPress posts are displayed chronologically, with the most recent first. Someone on the Genesis Facebook group asked for a change to this – blog page unchanged but category archives alphabetically!

pre_get_posts action

The ‘pre_get_posts‘ action is a little unusual as it behaves more like a filter – you can change the settings of the passed $query parameter, though you don’t return it.

Return early

The ‘pre_get_posts‘ action is run multiple times on each page load so hooked code should be as efficient as possible e.g. return early (end the function as soon as you determine that it does not need to continue.

The first step is the ensure we are not in the Dashboard (is_admin()) but are running the main query ($query->is_main_query()) and not a sidebar or other query.

Then we check that we are viewing the category we want to change (is_category(‘blog’)). Only then do we change the ‘orderby‘ and ‘order‘ parameters.

You can change those settings (or different settings) for your situation.

The code

Related Posts

How to use my code snippets

My code snippets are presented as small, single file plugins that are easy to use - you just upload and activate.

Sticky Posts In Category Archives

To make sticky posts work on archives I had to dive deep into the WP_Query::get_posts() code. And I was able to steal the hard part from WordPress.

Override Genesis front page template with code

Revert to a standard loop on the home page, overriding the theme front-page.php template without editing it.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Blog Categories

  • Being Green
  • Food
  • Grammar
  • Just Strange
  • Laziness
  • Personal
  • Portfolio
  • Race Reports
  • Shudder
  • Typo
  • Website Development
  • WooCommerce Tips
  • Facebook
  • Twitter

Copyright © 2013–2023 Damien Carbery · Using Genesis Framework · Privacy Policy