We recently had a customer request that the entries in a custom post type get automatically alphabetized. Clicking on the title certainly does the job here, but who’s got time for that?
There are a few ways to order posts, but if you want to specifically alphabetize a Custom Post Type, use the following snippet in functions.php
or a custom functions plugin:
// Alphabetize Post Type
function cws_custom_post_order( $query ) {
// Exit if not in WP Admin
if ( ! is_admin() ) {
return;
}
// Set variable for post_type
$post_type = $query->get('post_type');
// Conditional for correct post_type
if ( $post_type == 'manufacturer' ) {
// Orderby column
if ( $query->get( 'orderby' ) == "") {
$query->set( 'orderby', 'title' );
}
// Post order
if( $query->get( 'order' ) == "" ){
$query->set( 'order', 'ASC' );
}
}
}
add_action( 'pre_get_posts', 'cws_custom_post_order' );
If you need different parameters and don’t know what to adjust, leave a comment and I’ll help you out.