Sort scheduled posts in ascending order by default

I have a self-hosted WordPress blog (5.9). When I go to view scheduled posts, I want the default sort order to have the soonest scheduled posts on top.

I can do this by manually hitting the date column header, or by visiting /wp-admin/edit.php?post_status=futurepost_type=postorderby=dateorder=asc

I have lots of posts scheduled for the far future. But I mostly want to see what I've got being published next week.

Is there an action I can add to my theme - or a setting I can change - which alters the default sort for scheduled posts only?

Topic scheduled-posts sort Wordpress

Category Web


When I go to view scheduled posts, I want the default sort order to have the soonest scheduled posts on top.

The posts by default are indeed being sorted by the post date in descending order, so if you want the order be ascending instead, then there's no admin setting for that, but you can use the pre_get_posts hook like so:

add_action( 'pre_get_posts', 'my_admin_pre_get_posts' );
function my_admin_pre_get_posts( $query ) {
    // Do nothing if the post_status parameter in the URL is not "future", or that
    // a specific orderby (e.g. title) is set.
    if ( ! isset( $_GET['post_status'] ) || 'future' !== $_GET['post_status'] ||
        ! empty( $_GET['orderby'] )
    ) {
        return;
    }

    // Modify the query args if we're on the admin Posts page for managing posts
    // in the default "post" type.
    if ( is_admin() && $query->is_main_query() &&
        'edit-post' === get_current_screen()->id
    ) {
        $query->set( 'orderby', 'date' ); // default: date
        $query->set( 'order', 'ASC' );    // default: DESC
    }
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.