# Sort a Category Archive Alphabetically

> Orders posts in chosen categories by title from A to Z, which suits glossaries, directories and recipe indexes.

- Type: PHP
- Runs: Run everywhere
- Category: [Archives](https://wpide.com/library/category/archive/)
- Tags: archive, sorting, category
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/alphabetical-category-archive/

## Code

```php
add_action( 'pre_get_posts', function ( $query ) {
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }

    // Category slugs to sort alphabetically.
    $categories = array( 'glossary' );

    if ( $query->is_category( $categories ) ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
} );
```

## Notes

Replace glossary with your category slugs.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Sort a Category Archive Alphabetically" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
