Tired of whatever silly color I've picked? Rotate the overall hue by hovering the site title!

Move drafts to the top of your WordPress posts list

This isn't something WP supports by default as it has a strict list of available options for its orderby parameter and post_status ain't one of 'em.

Well, let's fix that. Yes, your posts drafts will probably be near the top anyway with the default descending date sorting, but sometimes things fall behind. Your page drafts, on the other hand, will be scattered about, so it's helpful for those! I haven't necessarily fully tested this yet, but it's working for me on this very site. And, yes, paging through your lists works too.

Here's the code to drop into functions.php or your preferred spot:

add_filter('posts_orderby', function ($orderby) {
    if (! is_admin())                         return $orderby;
    if (! $GLOBALS['pagenow'] === 'edit.php') return $orderby;

    return 'post_status ASC, ' . $orderby;
});

Feel free to rewrite the guard clauses in your preferred style. You can also add a second parameter to the filter, the relevant WP_Query object, and only apply this to certain post types. The above code will run on all of them.

Since we'll be instructing WP to deal with the post status as an ascending string (i.e. alphabetically), the order of your posts will go like this:

  • Drafts
  • Scheduled
  • Private
  • Published

Why is "Scheduled" higher than "Published"? Because behind the scenes the status is actually future rather than scheduled. #tmyk

Leave a comment

Your email address will not be published. Required fields are marked *