Sorting category pages alphabetically

In a Facebook group someone wanted to know how to order the posts on a category page alphabetically. In this blog post want to show, how you can easily sort posts on a category (or any other) archive page.

Sorting post on all category pages

Even though I don’t know why someone would like to order posts by name (archives of a different post type would probably make more sense), this is the code to order them:

function aa_sort_all_archives( $query ) {
	// Only sort main query.
	if ( ! $query->is_main_query() ) {
		return;
	}
	// Only sort category archives.
	if ( ! is_category() ) {
		return;
	}

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

add_action( 'pre_get_posts', 'aa_sort_all_archives' );

Anytime you want to alter the query, you would preferably use a callback function to the pre_get_posts hook. In this callback you would first check, if the main query is run. Then you check for what ever condition makes sense for your use case. If this condition is not met, exit the function. If all check succeeded, alter the query. So in line 7 we check, if we are on a category archive. If so, we change the order of posts to “ascending” (line 11) and the field by which we want to sort (line 12) to the post_title field. That’s all.

Order only posts in a specific category

As for some post types it might be unlikely, that you want to sort posts of all categories, you can also pass the category slug (or it’s name, ID) to the is_category() function. In this example, I’m using a dedicated category with the slug “alphabetical” and sort only posts on this archive page:

function aa_sort_alphabetical_archives( $query ) {
	// Only sort main query.
	if ( ! $query->is_main_query() ) {
		return;
	}
	// Only sort category archives.
	if ( ! is_category( 'alphabetical' ) ) {
		return;
	}

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

add_action( 'pre_get_posts', 'aa_sort_alphabetical_archives' );

In the same way, you can have check for many other archive pages using different “Conditional Tags“. In the CODEX you will also find a page about “Alphabetizing Posts” but for category pages it tells you to use a secondary query. Please never do this! And also never show all posts! Just don’t do, what the page tells you 😉

Conclusion

Sorting posts (or other post types) on an archive page is best done by using the pre_get_posts hook. When you first use this hook, it might not work in the way you want it to work, but it’s worth learning how to use it correctly.

You can find the code of this blog post as a working plugin in a GIST where you can also download it as a ZIP and install it to your site.

Posted by

Bernhard is a full time web developer who likes to write WordPress plugins in his free time and is an active member of the WP Meetups in Berlin and Potsdam.

1 comment » Write a comment

  1. From last 3 days browsing google for this solution finally, I got here Thank You So Much it worked perfectly how I wanted 🙂

Leave a Reply

Your email address will not be published.