Skip to content

Instantly share code, notes, and snippets.

@dartiss
Last active September 7, 2018 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dartiss/3cb34b86550714581ef3c223591c0787 to your computer and use it in GitHub Desktop.
Save dartiss/3cb34b86550714581ef3c223591c0787 to your computer and use it in GitHub Desktop.
Add a link to draft posts in your WordPress admin
<?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' );
@dartiss
Copy link
Author

dartiss commented Sep 7, 2018

Add to your theme's functions.php file to add a direct link to your draft posts to the WordPress menu.

Full details here - https://artiss.blog/2018/09/adding-a-link-to-your-draft-posts-in-the-wordpress-admin/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment