Skip to content

Instantly share code, notes, and snippets.

@mahodder
Created January 24, 2014 03:47
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mahodder/8591750 to your computer and use it in GitHub Desktop.
Save mahodder/8591750 to your computer and use it in GitHub Desktop.
Reverse WordPress post order on archive pages for specific categories only - add to functions.php
add_action( 'pre_get_posts', 'custom_reverse_post_order' );
function custom_reverse_post_order( $query ) {
if ( is_admin() )
return;
if ( $query->is_main_query() && is_archive() && ! is_post_type_archive() && ($query->query_vars['category_name'] == 'category-name' || $query->query_vars['category_name'] == 'category-name') ) {
$query->set( 'orderby', 'date' );
$query->set( 'order', 'ASC' );
}
}
@yonkov
Copy link

yonkov commented Nov 28, 2019

Very Nice! I have also developped a small plugin that lets you reverse the post order for specific categories via the WordPress dashboard: https://wordpress.org/plugins/post-order-by-category/

@iamwoodruff
Copy link

iamwoodruff commented Mar 24, 2020

Dude, this is soooo helpful. I had been stuck using a high-latency plugin (that undesirably affected all category archives) when this php blurb does exactly what I wanted with no significant overhead. Wish I had found it a year ago. THANK YOU SO MUCH!

@iamwoodruff
Copy link

Followup: can this be adapted so as to only sort the order of sticky posts on a theme configured to display all posts, starting with most recent, as the landing page?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment