Skip to content

Instantly share code, notes, and snippets.

@mayeenulislam
Last active July 13, 2021 22:27
Show Gist options
  • Save mayeenulislam/3b19e878ba784dc168f4 to your computer and use it in GitHub Desktop.
Save mayeenulislam/3b19e878ba784dc168f4 to your computer and use it in GitHub Desktop.
Show a pending posts count on Custom Post Type menu label like Plugin Update Notification count. Thanks to Samik Chattopadhyay for the nice snippet.
/**
* Get posts pending count as per Post Type.
* @param string $post_type
* @return integer Pending count.
*/
function project_get_pending_items( $post_type ) {
global $wpdb;
$pending_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s AND post_status = 'pending'", $post_type ) );
return (int) $pending_count;
}
/**
* Show pending count on menu.
* @return integer pending count beside menu label.
* @author Samik Chattopadhyay
* @link http://stackoverflow.com/a/8625696/1743124
*/
function notification_bubble_in_my_custom_post_type_menu() {
global $menu;
$pending_items = project_get_pending_items( 'my_custom_post_type' );
//while registering a post type the 'menu_position' is important, here our value was 7
$menu[7][0] .= $pending_items ? " <span class='update-plugins count-1' title='title'><span class='update-count'>$pending_items</span></span>" : '';
}
add_action('admin_menu', 'notification_bubble_in_my_custom_post_type_menu');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment