Sort a Post Type Archive A to Z
Lists the items of chosen post type archives in alphabetical order by title instead of newest first, for directories, glossaries and catalogs.
Opens WPIDE on
Code
// Post type archives sorted by title.
$wpide_az_types = array( 'glossary_term', 'location', 'partner' );
add_action( 'pre_get_posts', function ( $query ) use ( $wpide_az_types ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_post_type_archive( $wpide_az_types ) && ! $query->get( 'orderby' ) ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
} );
Notes
Their taxonomy archives keep the default order.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.