Tutorials

How to sort a post type alphabetically

By default, WordPress sorts all post types chronologically by date, like regular posts. This is a good default, but not always the desired result for some post types. For instance, the tutorials on this site are not really date-dependent, and therefore, it makes little sense to always show the latest one first. Instead, I sort all tutorials alphabetically, from A to Z.

There are two ways to do it: with the Query Loop block in the block editor, or with a hook in your theme. The block editor is the easiest option, but it only works for Query Loop blocks with their own query. For the archive of a post type, you’ll need the hook.

In the block editor

  1. Open the post, page, or template with the Query Loop block you want to sort, and click on the Query Loop block. You need to have the Query Loop itself selected, rather than any of the blocks inside of it. If you struggle to find it, you can expand the Document Overview by clicking the list icon in the top left, and then click on the Query Loop block in the list.
  2. In the block settings in the right sidebar, make sure that “Query type” is set to “Custom”.
  3. Change the “Order by” setting to “A → Z”.
  4. Save your changes.

If “Query type” is set to “Default”, the Query Loop shows the posts that WordPress has queried for the current page, like on the archive for a post type, and the “Order by” setting isn’t available. In archive templates, the Query Loop needs to follow the current page, so that’s where the hook below comes in.

With a hook in your theme

To do it for the archive of a post type, we’re going to use a hook in WordPress called pre_get_posts. The pre_get_posts hook allows us to modify how the WordPress query is structured in any given situation, but it’s best suited to making changes on archive pages. It can be used to exclude certain categories, only include content published within a certain date span, or change how the content of the query is ordered when it is output.

We’re doing the latter in the code below, which should go into the functions.php file of your theme. Replace xx with the slug for your project, and replace post_type_name with the name of the post type you want ordered alphabetically. If you’re using a theme made by someone else, add the code to a child theme or a small plugin instead, since any changes you make to the theme’s functions.php file will be lost the next time it’s updated.

function xx_sort_post_type_by_title( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_post_type_archive( 'post_type_name' ) ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'xx_sort_post_type_by_title' );

Let’s go through it, step by step. First, we tell WordPress to run the function xx_sort_post_type_by_title() when the pre_get_posts hook is called. WordPress passes along a variable called $query to the function. It holds the WordPress query object, which specifies what content WordPress should query and how.

function xx_sort_post_type_by_title( $query ) {
// Code
}
add_action( 'pre_get_posts', 'xx_sort_post_type_by_title' );

The pre_get_posts hook runs for every query on your site, both on the front end and in the admin. That’s why we start by stopping the function early if we’re in the admin, using the is_admin() conditional tag, or if the query isn’t the main query for the page. Without those checks, the post type would also be sorted alphabetically in the admin, and in other lists of posts on the page – like widgets, or Query Loop blocks with their own order settings.

if ( is_admin() || ! $query->is_main_query() ) {
return;
}

Next, we check that we’re on the archive for the post type we want to sort, so that other post types – like posts and pages – aren’t unintentionally sorted alphabetically. If the post type has its own categories or tags, you can add || $query->is_tax( 'taxonomy_name' ) to the check to sort those archives as well.

if ( $query->is_post_type_archive( 'post_type_name' ) ) {
// Code
}

Regular blog posts don’t have a post type archive, so if you want to sort them, replace $query->is_post_type_archive( 'post_type_name' ) with $query->is_home():

if ( $query->is_home() ) {
// Code
}

Finally, we tell the query to sort the posts by title in ascending order (A–Z).

$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );

That’s it! If you’ve followed the steps above correctly, the post type you’ve specified should now be sorted in alphabetical order rather than by publishing date.

Published November 2, 2026

Modified December 1, 2026