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
Add a comment  | 

1 Answer 1

Reset to default 3

There'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