25% off ProSNIPC25OFF

Sort Admin Post List Alphabetically by Title

Sets the default ordering of the Posts list table in wp-admin to A-Z by title when no custom sort column is active.

PHPby SnipCraft
php
<?php
add_action( 'pre_get_posts', 'scseed_sort_posts_list_alpha' );
if ( ! function_exists( 'scseed_sort_posts_list_alpha' ) ) {
    function scseed_sort_posts_list_alpha( $query ) {
        if ( ! is_admin() || ! $query->is_main_query() ) {
            return;
        }
        // Respect user-chosen column sorts.
        if ( $query->get( 'orderby' ) ) {
            return;
        }
        $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
        if ( ! $screen || 'edit' !== $screen->base ) {
            return;
        }
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
#admin#list-table#posts#sorting#ux