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:
- 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
Related Posts:
- pre_get_posts with get_posts
- How-to exclude terms from the main query the most performant way?
- “pre_get_posts” firing on every query
- Identify which loop you are hooking into; primary or secondary?
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- How to choose between hooking into per_get_posts or into parse_query
- Show Sticky Post at the top but do not show again in the loop?
- Can I set my WP_Query to be a Main Query?
- Show scheduled posts in main loop but not in WP_Query?
- WP Query – order posts by meta field first and then order the rest
- queried_object using pre_get_posts gets notices and warning
- Elementor posts custom query based on tag [closed]
- Can I alter the main loop to ‘orderby’ a custom callback?
- How to choose between hooking into pre_get_posts or into parse_query
- difference between querying database and using the loop
- If have posts a week old, display, else display a different loop
- WP_Query – Adding “offset” posts to the end of the loop
- Stomping WP_Query in author archive to facilitate pagination with custom queries
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- Conditional operator OR not working with custom fields
- Loop increase in while loop not working
- Create a loop on my pages with new “WP_Query”
- Get posts that do not have the same tags as current
- Utilising an existing page while using the “s” query parameter
- date_query in pre_get_posts out of memory
- posts archive page – closing WP_Query loop correctly [closed]
- How I can change the condition or compare operator for WP_Query in pre_get_posts
- Create a page template for “top rated posts” but show full content and not just a list
- has_excerpt() not working inside wp_query
- wp_query is showing posts from other categories
- Daily drip of posts – based on user ‘startData’ – ordered oldest to newest
- How to add tax_query to $args with concatenation
- Should I reset $wp_query?
- Meta query compare for ID’s greater than specific ID
- Exclude a Woocommerce product from WP_Query
- WP ForLoop to compare meta information of posts to determine what post to display
- AJAX Breaking Offset Argument In WP Query
- WP_QUERY wrong ammount of posts
- How to add sort order to incremented and paginated category loop
- How does WP generate the default $query in WP_Query based on the URL?
- Continue or break the while loop
- How to filter query loop block with a search string from the query parameters
- WP_Query – How to get all posts of specific days of week by custom field date?
- inserting content of 1 Post to in another with a template hierarchy
- how to get category`s slug in WP_Query loop?
- Loop posts based on permalink term
- Save (and exclude) posts from loop to use in another loop on page
- Multiple meta_key ordering with pre_get_posts
- Get all posts with empty meta_value
- Sorting search results with custom dropdown
- How to split a post and intercalate elements from a loop
- How to remove only the latest sticky post from the loop
- Expecting statement error from php loop using ACF plugin
- Loop 3 posts column wise and continue the loop into same columns
- How I can repeat 2 HTML templates in a WordPress Query?
- PHP – Loop custom post type categories within jQuery Tabs
- pre_get_posts – editing query, tax_query
- Check if a post has term inside loop
- How to create a widget like this?
- How do I sort this custom list of sticky posts
- WP_Query with one category in args shows other categories
- Query only displays one page_id
- How to Order By Two Custom Fields?
- Display specific page (that is child) content on parent page
- Different Loop for tag pages?
- Child pages not affected by orderby
- Ordering posts by custom field and grouped by month
- Pagination for custom loop on custom page template is not displaying anything
- Does putting queries within loops cause an issue on WordPress?
- Filtering by multiple conditions in the loop
- Hiding carousel indicators if there’s only one post
- Query Taxonomy By Page Title
- How to access $wp_query object from array of posts given by get_posts?
- Function using get_posts() with tax_query not working when called from functions.php
- Display all attached image of every post of custom post type and link to original post
- 2 queries with counters
- Get posts with no tags?
- wp-query, pull children of parent page
- relation OR instead of AND – Filtered term ID’s in loop
- Writing less unnecessary code with WordPress
- Does query_posts have an effect on get_the_category?
- Second loop pagination changes URL, not content
- How to change the default post type over the loop?
- I am officially missing something about transient posts
- Query Not working as expected
- Compare “Main” post ID to ID inside wp_query loop
- Offset WP_Query by negative 1
- WP_Query orderby random do not repeat infinite scroll – one loop
- Paginate pages with dynamic query
- WP_Query loop doen’t work with my custom taxonomy
- Advise on Templates for Custom Queries
- Search.php – return number of results but cannot loop through
- How do I get the content of a custom instance of WP_Query?
- Performance concerns: index.php vs taxonomy-$taxonomy.php
- Creating ‘posts page’ loop based on the page itself
- Why does apply_filters behave different inside and outside a loop?
- WordPress Queries Not Working?
- Determine if ID is page or post and query the ID
- Custom template for password protected page
- How to apply pre_get_posts to a custom query?