Created
April 16, 2017 20:56
-
-
Save douglasanro/fb1075980e5ca70e470d743417590e76 to your computer and use it in GitHub Desktop.
Add CPT count action to WordPress dashboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* ---------------------------------------------------------------------------- | |
| * Add CPT count action to WordPress dashboard | |
| * ------------------------------------------------------------------------- */ | |
| add_action( 'dashboard_glance_items', 'simple_glance_items' ); | |
| // Showing all custom posts count | |
| function simple_glance_items() { | |
| $glances = array(); | |
| $args = array( | |
| 'public' => true, // Showing public post types only | |
| '_builtin' => false // Except the build-in wp post types (page, post, attachments) | |
| ); | |
| // Getting your custom post types | |
| $post_types = get_post_types( $args, 'object', 'and' ); | |
| foreach ( $post_types as $post_type ) { | |
| // Counting each post | |
| $num_posts = wp_count_posts( $post_type->name ); | |
| // Number format | |
| $num = number_format_i18n( $num_posts->publish ); | |
| // Text format | |
| $text = _n( $post_type->labels->singular_name, $post_type->labels->name, intval( $num_posts->publish ) ); | |
| // If use capable to edit the post type | |
| if ( current_user_can( 'edit_posts' ) ) { | |
| // Show with link | |
| $glance = '<a class="'.$post_type->name.'-count" href="'.admin_url( 'edit.php?post_type='.$post_type->name ).'">'.$num.' '.$text.'</a>'; | |
| } else { | |
| // Show without link | |
| $glance = '<span class="'.$post_type->name.'-count">'.$num.' '.$text.'</span>'; | |
| } | |
| // Save in array | |
| $glances[] = $glance; | |
| } | |
| // return them | |
| return $glances; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment