wp_count_posts

Count number of posts of a post type and if user has permissions to view.

Description

(object) wp_count_posts( (string) $type = 'post', (string) $perm = '' ); 

This function provides an efficient method of finding the amount of post's type a blog has. Another method is to count the amount of items in get_posts(), but that method has a lot of overhead with doing so. Therefore, when developing for 2.5+, use this function instead.

The $perm parameter checks for readable value and if the user can read private posts, it will display that for the user that is signed in.

Returns (object)

Number of posts for each status.

Parameters (2)

0. $type — Optional. (string) => 'post'
Post type to retrieve count. Default post..
1. $perm — Optional. (string) => ''
readable or empty. Default empty.

Usage

  1. if ( !function_exists( 'wp_count_posts' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Optional. Post type to retrieve count. Default 'post'. 
  5. $type = 'post'; 
  6.  
  7. // Optional. 'readable' or empty. Default empty. 
  8. $perm = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_count_posts($type, $perm); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post.php  
  1. function wp_count_posts( $type = 'post', $perm = '' ) { 
  2. global $wpdb; 
  3.  
  4. if ( ! post_type_exists( $type ) ) 
  5. return new stdClass; 
  6.  
  7. $cache_key = _count_posts_cache_key( $type, $perm ); 
  8.  
  9. $counts = wp_cache_get( $cache_key, 'counts' ); 
  10. if ( false !== $counts ) { 
  11. /** This filter is documented in wp-includes/post.php */ 
  12. return apply_filters( 'wp_count_posts', $counts, $type, $perm ); 
  13.  
  14. $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; 
  15. if ( 'readable' == $perm && is_user_logged_in() ) { 
  16. $post_type_object = get_post_type_object($type); 
  17. if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) { 
  18. $query .= $wpdb->prepare( " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",  
  19. ); 
  20. $query .= ' GROUP BY post_status'; 
  21.  
  22. $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); 
  23. $counts = array_fill_keys( get_post_stati(), 0 ); 
  24.  
  25. foreach ( $results as $row ) { 
  26. $counts[ $row['post_status'] ] = $row['num_posts']; 
  27.  
  28. $counts = (object) $counts; 
  29. wp_cache_set( $cache_key, $counts, 'counts' ); 
  30.  
  31. /** 
  32. * Modify returned post counts by status for the current post type. 
  33. * @since 3.7.0 
  34. * @param object $counts An object containing the current post_type's post 
  35. * counts by status. 
  36. * @param string $type Post type. 
  37. * @param string $perm The permission to determine if the posts are 'readable' 
  38. * by the current user. 
  39. */ 
  40. return apply_filters( 'wp_count_posts', $counts, $type, $perm );