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.
Leave a Reply