最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Sort scheduled posts in ascending order by default

programmeradmin2浏览0评论

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=future&post_type=post&orderby=date&order=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?

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=future&post_type=post&orderby=date&order=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?

Share Improve this question edited Feb 26, 2022 at 15:08 Terence Eden asked Feb 26, 2022 at 12:19 Terence EdenTerence Eden 1577 bronze badges 1
  • 1 You might want to edit the title of your question to use "ascending" instead.. – Sally CJ Commented Feb 26, 2022 at 14:25
Add a comment  | 

1 Answer 1

Reset to default 1

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
    }
}
发布评论

评论列表(0)

  1. 暂无评论