Adding a link to your draft posts in the WordPress admin

When you’re working in a publishing environment, the most used part of WordPress will be viewing your draft posts. After all, this is your current, active work. Once done, it’s over to an editor to publish. Yet, draft posts are a few too many clicks than they really need to be.

The following code, added to your theme’s functions.php file will add new sub-menu options under the “Post” menu.

<?php
function add_drafts_to_menu() {
global $wpdb;
$author = get_current_user_id();
// Get total number of draft posts. If more than zero add a sub-menu option
$all_posts = $wpdb -> get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft'" );
if ( $all_posts > 0 ) {
add_submenu_page( 'edit.php', '', __( 'All Drafts <span class=\'update-plugins count-' . $all_posts . '\'><span class=\'update-count\'>' . $all_posts . '</span></span>' ), 'edit_posts', esc_url( 'edit.php?post_status=draft&post_type=post' ) );
// Get total number of draft posts for current user. If more than zero add a sub-menu option
$your_posts = $wpdb -> get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author=" . $author );
if ( $your_posts > 0 && $your_posts !== $all_posts ) {
add_submenu_page( 'edit.php', '', __( 'My Drafts <span class=\'update-plugins count-' . $your_posts . '\'><span class=\'update-count\'>' . $your_posts . '</span></span>' ), 'edit_posts', esc_url( 'edit.php?post_status=draft&post_type=post&author=' . $author . '' ) );
}
}
}
add_action( 'admin_menu', 'add_drafts_to_menu' );
view raw draft-menu.php hosted with ❤ by GitHub

If any drafts exist, you’ll see a sub-menu named “All drafts”. If you, personally, have any drafts then you’ll see a second sub-menu named “My drafts”. Alongside each, too, is a counter of how many drafts there are.

If you think of any improvements to this, please let me know.

Talk to me!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from David Artiss

Subscribe now to keep reading and get access to the full archive.

Continue reading