Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Change post order by category - Order a specific category by date, rest alphabetically. https://www.damiencarbery.com/2020/08/change-post-order-by-category/
<?php
/*
Plugin Name: Change post order by category
Plugin URI: https://www.damiencarbery.com/2020/08/change-post-order-by-category/
Description: Order a specific category by date, the rest alphabetically.
Author: Damien Carbery
Version: 0.1
*/
// Change the post order to ascending by title for all categories except the 'blog' category.
add_action( 'pre_get_posts', 'dcwd_change_post_order_by_category' );
function dcwd_change_post_order_by_category( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( ! is_category( 'blog' ) ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'asc' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.