Question

I've been trying to get a pending count to appear on the admin sidebar, for pending posts, like the little bubble that appears for pending comments:

Comments pending bubble

Offtopic: Am I the only one that thinks this should be core behaviour? Where should I suggest this feature?

Anyhow, I found this plugin, but I noticed it didn't always worked. Sometimes the notifier appears on Pages or other item.

The code it uses to add the pending count goes something like this:

$menu[5][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n($pending_count) . '</span></span>';

So, clearly the problem is the hard-coded 5 there, but how can I update it so it always points to Posts?

I'll be glad to commit this change to the plugin if we know the answer.

Thanks!

Was it helpful?

Solution

@ign

Replace the line of code you posted with the following..

foreach( $menu as $menu_key => $menu_data ) :
    if( 'edit.php' != $menu_data[2] )
        continue;
    $menu[$menu_key][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n($pending_count) . '</span></span>';
endforeach;

..that should avoid the need to know the specific key.. (let me know if any problems)..

Hope that helps.. :)

OTHER TIPS

as a followup to t31os' answer, here's the complete code needed (combining the contents of the plugin mentioned with t31os' fix), with modifications to handle custom post types as well:

add_filter( 'add_menu_classes', 'show_pending_number');
function show_pending_number( $menu ) {
    $type = "animals";
    $status = "pending";
    $num_posts = wp_count_posts( $type, 'readable' );
    $pending_count = 0;
    if ( !empty($num_posts->$status) )
        $pending_count = $num_posts->$status;

    // build string to match in $menu array
    if ($type == 'post') {
        $menu_str = 'edit.php';
    } else {
        $menu_str = 'edit.php?post_type=' . $type;
    }

    // loop through $menu items, find match, add indicator
    foreach( $menu as $menu_key => $menu_data ) {
        if( $menu_str != $menu_data[2] )
            continue;
        $menu[$menu_key][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n($pending_count) . '</span></span>';
    }
    return $menu;
}

place this in functions.php, no need for the plugin.

I made a slight alteration to somatic's post which allows for multiple post types:

// Add pending numbers to post types on admin menu
function show_pending_number($menu) {    
    $types = array("post", "page", "custom-post-type");
    $status = "pending";
    foreach($types as $type) {
        $num_posts = wp_count_posts($type, 'readable');
        $pending_count = 0;
        if (!empty($num_posts->$status)) $pending_count = $num_posts->$status;

        if ($type == 'post') {
            $menu_str = 'edit.php';
        } else {
            $menu_str = 'edit.php?post_type=' . $type;
        }

        foreach( $menu as $menu_key => $menu_data ) {
            if( $menu_str != $menu_data[2] )
                continue;
            $menu[$menu_key][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n($pending_count) . '</span></span>';
            }
        }
    return $menu;
}
add_filter('add_menu_classes', 'show_pending_number');
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top