Custom Post Type sorted by Title

I created a non-hierarchical custom post type and, by default, it’s sorted by date published.

I know I can reorder them by Title with query_posts() in the archive template:

global $query_string;
query_posts( $query_string . '&orderby=title&order=ASC' );

but it takes another SQL query on each archive page. Is there a way to register the sort order natively, eventually to get posts sorted by title even in the admin?

1 Answer
1

Try…

add_filter("posts_orderby", "my_orderby_filter", 10, 2);

function my_orderby_filter($orderby, &$query){
    global $wpdb;
    //figure out whether you want to change the order
    if (get_query_var("post_type") == "my_post_type") {
         return "$wpdb->posts.post_title ASC";
    }
    return $orderby;
 }

Leave a Comment