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 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.
Share Improve this question edited Feb 6, 2019 at 9:19 Krzysiek Dróżdż 25.5k9 gold badges53 silver badges74 bronze badges asked Feb 6, 2019 at 8:01 BikramBikram 3386 silver badges22 bronze badges 5- Please edit the question giving us information on how do you retrieve posts on these pages. – Max Yudin Commented Feb 6, 2019 at 8:05
- Man! I don't understand that much WordPress coding. I have only styled the archive.php no change in function. The post on my categories orders in Ascending and order by date. They are ordered on basis of new dates to older dates. Now, I want them to be in descending. – Bikram Commented Feb 6, 2019 at 8:14
- Having no coding experience you have to search for a plugin to achieve the goal. – Max Yudin Commented Feb 6, 2019 at 8:36
- No problem dude, I found an answer to this question wordpress.stackexchange/questions/39817/…. I put the order to my desire. Answered by Stephen Harris. – Bikram Commented Feb 6, 2019 at 8:41
- If you are interested in a plugin-like solution: I would recommend any of these two plugins: Chronological Posts (reverses post order for all category archives) Post Order By Category (reverses the post order for a specific category) – Badan Commented Nov 27, 2019 at 21:47
1 Answer
Reset to default 9You 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:
- We're adding our function that will modify params for WP_Queries
- We make sure that it will change only main query (the one created by WordPress) and only on category and tag listings (
is_category()
andis_tag()
). - We change the order of posts for these pages by setting
order
toASC