admin管理员组文章数量:1336632
This code I have is working but I am not sure if it is the right way to do this. Basically when a user adds a record to a custom post type it goes straight to pending and needs an admin to publish it. I want the admin user to know their are records for them to approve by showing a red badge with a number in it, the same as when a plugin needs an update.
As mentioned, this works but maybe there is a better, more wordpress way to do it than running a custom SQL query?
add_action( 'admin_menu', 'add_user_menu_bubble' );
function add_user_menu_bubble() {
global $wpdb;
global $menu;
$memo_count = $wpdb->get_var(" SELECT COUNT(*) FROM $wpdb->posts WHERE `post_status` = 'pending' AND `post_type` = 'memo'");
if ( $memo_count ) {
foreach ( $menu as $key => $value ) {
if ( $menu[$key][2] == 'edit.php?post_type=memo' ) {
$menu[$key][0] .= ' <span class="update-plugins">' . $memo_count . '</span>';
return;
}
}
}
}
This code I have is working but I am not sure if it is the right way to do this. Basically when a user adds a record to a custom post type it goes straight to pending and needs an admin to publish it. I want the admin user to know their are records for them to approve by showing a red badge with a number in it, the same as when a plugin needs an update.
As mentioned, this works but maybe there is a better, more wordpress way to do it than running a custom SQL query?
add_action( 'admin_menu', 'add_user_menu_bubble' );
function add_user_menu_bubble() {
global $wpdb;
global $menu;
$memo_count = $wpdb->get_var(" SELECT COUNT(*) FROM $wpdb->posts WHERE `post_status` = 'pending' AND `post_type` = 'memo'");
if ( $memo_count ) {
foreach ( $menu as $key => $value ) {
if ( $menu[$key][2] == 'edit.php?post_type=memo' ) {
$menu[$key][0] .= ' <span class="update-plugins">' . $memo_count . '</span>';
return;
}
}
}
}
Share
Improve this question
asked May 21, 2020 at 15:14
user8463989user8463989
5931 gold badge8 silver badges24 bronze badges
1 Answer
Reset to default 3There's indeed a WordPress function for counting posts: wp_count_posts()
. So you can do something like:
$memo_count = wp_count_posts( 'memo' )->pending;
本文标签: wpdbshow badge with count for pending items in custom post type
版权声明:本文标题:wpdb - show badge with count for pending items in custom post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742411692a2469918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论