Content Marketing

WordPress: How To Sort Your Custom Post Type Posts Alphabetically

With the new theme (and child theme) I’ve implemented on Martech Zone, I had to rebuild and recode the custom post type I built for Acronyms. I optimized the code to insert some additional custom fields and I’m having to redesign the archive and taxonomy templates to better display the acronyms listed.

In my last theme (whose developers discontinued support), these pages got quite a bit of attention because they were well-documented and even showed relevant articles to the acronym. I’ll continue to migrate that functionality to the new site and I even want to use a hover methodology to display the acronym definition rather than having the visitor click on the acronym link. Enough about that…

Custom Post Type Sorting

Because WordPress was originally designed for blog use, the default of any post type (including a custom post type) is to order the posts in reverse chronological order. While that works for news and articles, it’s not advantageous for things like a glossary or a list of acronyms. I want my acronyms to be ordered alphanumerically, not by the date that entered them in WordPress.

As with virtually every feature in WordPress, this can be easily customizable with the WordPress API. In my functions.php file in my child theme, I added the following code:

add_action( 'pre_get_posts', function ( $query ) {
	if ( $query->is_archive() && $query->is_main_query() ) { 
	  if ( get_query_var( 'post_type' ) == 'acronym' ) { 
		$query->set( 'order', 'ASC' );
		$query->set( 'orderby', 'title' );
	  };
	};
} );

The pre_get_posts function is an action that is executed whenever posts are queried. In the code above, I’m able to ensure that any query for the custom post type of acronym is specifically set to be sorted by the title in ascending order.

This doesn’t just set this order in the output of the archive and taxonomy pages, it even orders the custom post type alphanumerically within the administrative panel of WordPress.

Custom Post Type sorted alphabetically by title

Because you’re setting the default query parameters, you can add other variables as well, like the number of records to retrieve (posts_per_page). For acronyms, I’m returning 25 records at a time where I’m defaulting to 10 on the rest of the site.

If you’d like to just publish the custom post type alphabetically on your archive template but not in your WordPress administration panel, you can update that code to:

function acronym_archive_orderby_title($query) {
    // Check if it's the main query, an archive page, and the post type is 'acronym'
    if ($query->is_main_query() && is_post_type_archive('acronym') && !is_admin()) {
        // Order by title alphabetically
        $query->set('orderby', 'title');
        $query->set('order', 'ASC');
    }
}
add_action('pre_get_posts', 'acronym_archive_orderby_title');

Custom post types can help you to significantly expand your site’s capabilities… and it can all be done with some simple code within your child theme (or core theme) without the need for any plugins. In fact, I recommend not using plugins since they often add computing overhead that may slow your site. I’m working on a client site right now where they’d like to incorporate job openings… and this code is going to come in handy for them as well!

Douglas Karr

Douglas Karr is CMO of OpenINSIGHTS and the founder of the Martech Zone. Douglas has helped dozens of successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to assist companies in implementing and automating their sales and marketing strategies. Douglas is an internationally recognized digital transformation and MarTech expert and speaker. Douglas is also a published author of a Dummie's guide and a business leadership book.

Related Articles

Back to top button