WordPress by default order posts in descending order by date. But I want them to be in ascending order by date.
How do I turn the order to ascending only for tags and category? I don't want blog post orders to change.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityWordPress by default order posts in descending order by date. But I want them to be in ascending order by date.
How do I turn the order to ascending only for tags and category? I don't want blog post orders to change.
You can use pre_get_posts
action to modify this order. Here's the code:
function my_change_posts_order( $query ){
if ( ! is_admin() && ( is_category() || is_tag() ) && $query->is_main_query() ) {
$query->set( 'order', 'ASC' );
}
};
add_action( 'pre_get_posts', 'my_change_posts_order');
What we do here is:
is_category()
and is_tag()
).order
to ASC