Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@yonkov 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

This comment has been minimized.

Copy link

@iamwoodruff 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

This comment has been minimized.

Copy link

@iamwoodruff iamwoodruff commented Mar 25, 2020

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