sinanisler logo

Admin Dashboard Posts Order by Date, Newest to Oldest, WordPress

as you may know in wp admin posts or pages orders by little bit weird this code will ensures posts, pages and post types order newest to older in default. you can still filter order by clicking the columns titles as always if you wanted to.

function custom_admin_post_order( $wp_query ) {
    if (is_admin()) {
        // Get the post type from the query
        $post_type = $wp_query->query['post_type'];

        // Check if the current post type is 'post' or any custom post type
        if ( 'post' == $post_type || is_array($post_type) || is_string($post_type)) {
            // If 'orderby' is not set, set it to 'date'
            if (!isset($_GET['orderby'])) {
                $wp_query->set('orderby', 'date');
                $wp_query->set('order', 'DESC'); // 'DESC' for newest to oldest
            }
        }
    }
}

add_filter('pre_get_posts', 'custom_admin_post_order');


This snippet was created for one of the client project of geopard.digital.

Leave the first comment