1

I need all auto-generated category pages to sort oldest-to-newest

I'm editing my "All Archives" template in block editor but I'm not having any success

it contains a query loop with "inherit query from template" checked, and turning this off is not an option because it's a template used by all categories and if turned off I won't get the correct content on the correct pages

however, having this option checked also prevents changing the sort order

all the solutions I find online say to edit the theme's archives.php, however, this is a file that only exists in legacy themes and I'm using a modern theme

my theme does have an archives.html containing the following line:

<!-- wp:query {"query":{"perPage":10,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true},"tagName":"main"} -->

I changed desc to asc, cleared WP-Optimize cache, cleared Redis cache, cleared Cloudflare cache, restarted PHP-FPM, restarted Apache, restarted MySQL, restarted Redis, then cleared WP-Optimize and Redis caches again, ran a WP-Optimize database cleanup, restarted PHP/Apache/MySQL/Redis again, and then cleared all caches again. However, there was no change.

1 Answer 1

0

Given your detailed description, it seems you've taken several steps to address the issue of sorting your archive pages in WordPress. The fact that changing the "order":"desc" to "asc" in the archives.html file of your theme didn't yield the desired result suggests a deeper issue.

Try this in your functions.php file:

function wpb_modify_category_query( $query ) {
    if ( $query->is_category() && $query->is_main_query() ) {
        // Set the order to ASC
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'wpb_modify_category_query' );

1
  • 1
    That did the job! I reverted the archives.html back to its original state. Thank you very much. Commented Jan 27, 2024 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.