如何检查或计算 post "draft" 状态并在 wordpress 菜单旁边显示气泡

How do I check or count post "draft" status and display bubble next to the menu in wordpress

所以感谢 rudrastyh 我能够获得启用通知气泡的代码的主要部分,但还没有任何方法来检查和计算 draft 中的推荐仅状态(并在气泡中显示计数)我在 php 非常绿色,如果有人可以提供答案将不胜感激 - 提前致谢!

// ADD BUBBLES TO ADMIN_COOKIE_PATH

add_action( 'admin_menu', function() {

    global $menu;

    $count = 5;

    $menu_item = wp_list_filter(
        $menu,
        array( 2 => 'edit.php?post_type=testimonials' ) // 2 is the position of an array item which contains URL, it will always be 2!
    );

    if ( ! empty( $menu_item )  ) {
        $menu_item_position = key( $menu_item ); // get the array key (position) of the element
        $menu[ $menu_item_position ][0] .= ' <span class="awaiting-mod">' . $count . '</span>';
    }

});

试试这个

add_action( 'admin_menu', function() {

    global $menu;

    //$count = 5;
    $args = array(
        'numberposts'   => -1,
        'post_type'     => 'testimonials',
        'fields' => 'ids',
        'no_found_rows' => true,
        'post_status'   => array( 'draft' ),

    );
    $count_drafts = count( get_posts( $args ) ); 
    if($count_drafts > 0) {
  
    $menu_item = wp_list_filter(
        $menu,
        array( 2 => 'edit.php?post_type=testimonials' ) // 2 is the position of an array item which contains URL, it will always be 2!
    );

    if ( ! empty( $menu_item )  ) {
        $menu_item_position = key( $menu_item ); // get the array key (position) of the element
        $menu[ $menu_item_position ][0] .= ' <span class="awaiting-mod">' . $count_drafts . '</span>';
    }
    }
});

重要的细节,我们在查询中使用

'fields' => 'ids',
'no_found_rows' => true,

因为我们只需要计算计数,大大降低了数据库的负载,加快了查询速度