Title: _count_posts_cache_key
Published: April 25, 2014
Last modified: August 20, 2026

---

# _count_posts_cache_key( string $type = ‘post’, string $perm = ” ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/_count_posts_cache_key/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/_count_posts_cache_key/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/_count_posts_cache_key/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/_count_posts_cache_key/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_count_posts_cache_key/?output_format=md#changelog)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Returns the cache key for [wp_count_posts()](https://developer.wordpress.org/reference/functions/wp_count_posts/)
based on the passed arguments.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/_count_posts_cache_key/?output_format=md#parameters)󠁿

 `$type`stringoptional

Post type to retrieve count Default `'post'`.

Default:`'post'`

`$perm`stringoptional

`'readable'` or empty.

Default:`''`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/_count_posts_cache_key/?output_format=md#return)󠁿

 string The cache key.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/_count_posts_cache_key/?output_format=md#source)󠁿

    ```php
    function _count_posts_cache_key( $type = 'post', $perm = '' ) {
    	$cache_key = 'posts-' . $type;

    	if ( 'readable' === $perm && is_user_logged_in() ) {
    		$post_type_object = get_post_type_object( $type );

    		if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
    			$cache_key .= '_' . $perm . '_' . get_current_user_id();
    		}
    	}

    	return $cache_key;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/post.php#L3430)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/post.php#L3430-L3442)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_count_posts_cache_key/?output_format=md#related)󠁿

| Uses | Description | 
| [current_user_can()](https://developer.wordpress.org/reference/functions/current_user_can/)`wp-includes/capabilities.php` |

Returns whether the current user has the specified capability.

  | 
| [is_user_logged_in()](https://developer.wordpress.org/reference/functions/is_user_logged_in/)`wp-includes/pluggable.php` |

Determines whether the current visitor is a logged in user.

  | 
| [get_current_user_id()](https://developer.wordpress.org/reference/functions/get_current_user_id/)`wp-includes/user.php` |

Gets the current user’s ID.

  | 
| [get_post_type_object()](https://developer.wordpress.org/reference/functions/get_post_type_object/)`wp-includes/post.php` |

Retrieves a post type object by name.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/_count_posts_cache_key/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/_count_posts_cache_key/?output_format=md#)

| Used by | Description | 
| [_transition_post_status()](https://developer.wordpress.org/reference/functions/_transition_post_status/)`wp-includes/post.php` |

Hook for managing future post transitions to published.

  | 
| [wp_count_posts()](https://developer.wordpress.org/reference/functions/wp_count_posts/)`wp-includes/post.php` |

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

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/_count_posts_cache_key/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.9.0](https://developer.wordpress.org/reference/since/3.9.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_count_posts_cache_key%2F)
before being able to contribute a note or feedback.