How to Alphabetize Posts in WordPress

How to alphabetize posts in WordPress

Get started with the most secure WordPress hosting. Look no further than InMotion Hosting’s Managed WordPress Hosting solutions!

check markFree SSLs    check markMalware & Hack Protection    check markDDoS Protection

View WordPress Hosting Plans

In our WordPress tutorial series you have already seen how to install the WordPress core files and begin publishing content to your site in the form of posts, pages, media, etc. After reading this article you will know how to organize your posts alphabetically for your readers.

Why Alphabetize Posts in WordPress?

As you learn how to customize your WordPress templates to do more non-standard things, it’s a good idea to bookmark our complete guide on WordPress troubleshooting techniques.

By default, WordPress will display published posts in reverse chronological order. This can be changed by adding a snippet of code to the functions.php file in the WordPress core configuration.

For one reason or another you may want to change the default post display to alphabetize your posts. For example, if you have a company website for internal use only, it might help your users navigate documents in alphabetical order. Or, if you are writing a lexicon or dictionary, sorting posts this way will make navigation through your content easier.

Bear in mind, editing the source code of your WordPress installation can be hazardous. Make sure to copy the code samples and paste them exactly.

Editing the Functions File

You will need to access the functions.php file in the wp-content/themes folder for the WordPress theme you’re using. In this example, I’m using the default “twentysixteen” theme.

Here is the code you will need:

function foo_modify_query_order( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'foo_modify_query_order' );

Place the above snippet of text on the first available line after the initial comments.

Be sure to place your PHP code in PHP tags.

You can edit the functions.php file with any text editor or via FTP or the cPanel File manager.

After adding the code snippet, save the file and reload your WordPress front page. You’ll now notice your posts diplay in alphabetical order on your front page.

This modification can also be applied to individual categories.

Word of Warning

Whenever editing the source code of any WordPress installation it is always wise to save a backup of the original file. An accidental edit can inadvertently wipe out critical data.

In the case of WordPress themes, you can keep an untouched copy in a safe location to be restored if necessary; or, if possible, you can re-download the source files from the developer.

Check out these other resources from the support center:

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Let us know!