1

I'm currently building a plugin for a client, where users can book meeting-rooms from the frontend. Then AJAX will create a custom post type, and an email will be sent to the site administrator. All that is working fine. What I'm trying to implement now, is the orange number you usually see when there's a new comment or plugin-updates. How do I add that to my Custom Post Type, if a certain meta_key has a certain meta_value? Eg.

'booking_status' = 'pending'

Please let me know if you need any more information. Either I'm just bad at Googling, or else I'm not looking the right place.

2
  • the query to fetch all the posts that have that meta value will require a meta_value or a meta_query parameter which will be very slow, and get slower as the number of posts in the DB rises. This problem goes away if booking_status is a taxonomy and pending is a term in that taxonomy. This also has the benefit that WP already counts posts in terms, and the query will be orders of magnitude faster, nevermind that you won't even need to fetch the posts as just grabbing the term would give you the count/number to go in the badge
    – Tom J Nowell
    Mar 9, 2023 at 15:58
  • @TomJNowell - great idea! But my main question is, how do I make that orange dot appear next to my Bookings label in the controlpanel?
    – WPTricksDK
    Mar 9, 2023 at 16:53

1 Answer 1

1

I believe that the only sensible place to rename a menu item is the add_menu_classes filter hook.

It has a different destiny, but I can't see any other place. Hook passes an array of first-level menu items as a parameter to the function. The array elements look like this:

$menu = [
    5 => Array (
        [0] => 'Menu title'
        [1] => 'capabilities'
        [2] => 'menu_slug' // e.g. edit.php | edit.php?post_type=page | edit.php?post_type=cpt_slug 
        [3] => 'Page title'
        [4] => 'css classes'
        [5] => ??
        [6] => 'dash-icon'
    ),
    70 => Array (
        [0] => 'Users'
        [1] => 'list_users'
        [2] => 'users.php'
        [3] => ''
        [4] => 'menu-top menu-icon-users'
        [5] => 'menu-users'
        [6] => 'dashicons-admin-users'
    ),
]

You can add badge with number by editing the menu item title, which is at index [0].

add_filter( 'add_menu_classes', 'se414500_menu_classes' );

function se414500_menu_classes ($menu)
{
    // --- v1 ---
    $my_counter = count( $something );
    foreach($menu as &$item)
    {
        // use menu_slug of your item
        if ($item[2] != 'edit.php?post_type=my_cpt_slug')
            continue;

        $item[0] .= sprintf(
            '<span class="update-plugins count-%s"><span class="update-count">%s</span></span>',
            $my_counter,
            number_format_i18n( $my_counter )
        );
    }
    // --- v2 ---
    // foreach($menu as &$item)
    // {
    //     // use menu_slug of your item
    //     if ($item[2] == 'edit.php?post_type=my_cpt_slug') {
    //         //
    //         // get the value to display next to the name
    //         $my_counter = count( $something );
    //
    //         $item[0] .= sprintf(
    //             '<span class="update-plugins count-%s"><span class="update-count">%s</span></span>',
    //             $my_counter,
    //             number_format_i18n( $my_counter )
    //         );
    //     }
    // }

    return $menu;
}
1
  • Yep, that's it! Used WP_Query to fetch 'posts_found' (drafted posts) - and it works like a charm!
    – WPTricksDK
    Mar 14, 2023 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.