I'd like to add that nice little notification bubble beside a nav item in the admin. But I don't want to slow down the whole admin by triggering a post query every time just to display the value in the little bubble. Hoping to load this one integer value into transient cache to use for display in the menu.
Developing a custom plugin which registers a custom post type. This automatically creates the nav item in the admin, which I'd like to add the bubble to. The value within the bubble will be pulled from a simple query of how many posts (of this CPT) have been assigned to a custom taxonomy. For example if post has been assigned to custom term "pending review" then add to bubble count.
This function here works a charm, although I have no idea how to transition to using transient cache to store and retrieve the data.... any suggestions would be greatly appreciated. Thanks!
add_action( 'admin_menu', 'add_cpt_menu_bubble' );
function add_cpt_menu_bubble() {
global $menu;
$count_posts = 0;
// count the number of posts to show in bubble
$args = array(
'order' => 'DESC',
'posts_per_page' => '-1',
'post_type' => 'custom_post_type_name',
'custom_tax_name' => 'pending-review'
);
// The Query
query_posts( $args );
if (have_posts()) :
while ( have_posts() ) : the_post();
$count_posts++;
endwhile;
else:
endif;
wp_reset_query();
// only display the number of pending posts over a certain amount
if ( $count_posts > 5 ) {
foreach ( $menu as $key => $value ) {
if ( $menu[$key][2] == 'edit.php?post_type=custom_post_type_name' ) {
$menu[$key][0] .= ' <span class="update-plugins count-2"><span class="update-count">' . $count_posts . '</span></span>';
return;
}
}
}
} // EOF