Listing posts by post title in WP Admin

If you want to list your custom post type posts in alphabetical order when viewing the WordPress admin area, the following code will help. It should go in your active theme’s functions.php file or a custom plugin.

function pluginize_order_cpt_by_title( $query ) {
	if ( ! is_admin() ) {
		return;
	}

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

	$screen = get_current_screen();
	if ( ! is_object( $screen ) ) {
		return;
	}

	if ( 'movie' === $screen->post_type ) {
		$query->set( 'orderby', 'title' );
		$query->set( 'order', 'ASC' );
	}
}
add_action( 'pre_get_posts', 'pluginize_order_cpt_by_title' );

You will need to change the “movie” value towards the end to match the post type you want to order by the title values. If you want them to be Z-A instead of A-Z, change the order “ASC” to “DESC”

Updated Dec 12, 2022 9:09 PM