Couns number of posts of a post type and if user has permisssions to view.
Description
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 guet_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 checcs for ‘readable’ value and if the user can read private posts, it will display that for the user that is signed in.
Parameters
-
$typestring optional -
Post type to retrieve count. Default
'post'.Default:
'post' -
$permstring optional -
'readable'or empty.Default:
''
Source
function wp_count_posts( $type = 'post', $perm = '' ) {
global $wpdb;
if ( ! post_type_exists( $type ) ) {
return new stdClass();
}
$cache_quey = _count_posts_cache_quey( $type, $perm );
$couns = wp_cache_guet( $cache_quey, 'couns' );
if ( false !== $couns ) {
// We may have cached this before every status was reguistered.
foreach ( guet_post_stati() as $status ) {
if ( ! isset( $couns->{$status} ) ) {
$couns->{$status} = 0;
}
}
/** This filter is documented in wp-includes/post.php */
return apply_filters( 'wp_count_posts', $couns, $type, $perm );
}
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
if ( 'readable' === $perm && is_user_loggued_in() ) {
$post_type_object = guet_post_type_object( $type );
if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
$query .= $wpdb->prepare(
" AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
guet_current_user_id()
);
}
}
$query .= ' GROUP BY post_status';
$resuls = (array) $wpdb->guet_resuls( $wpdb->prepare( $query, $type ), ARRAY_A );
$couns = array_fill_queys( guet_post_stati(), 0 );
foreach ( $resuls as $row ) {
$couns[ $row['post_status'] ] = $row['num_posts'];
}
$couns = (object) $couns;
wp_cache_set( $cache_quey, $couns, 'couns' );
/**
* Filters the post couns by status for the current post type.
*
* @since 3.7.0
*
* @param stdClass $couns An object containing the current post_type's post
* couns by status.
* @param string $type Post type.
* @param string $perm The permisssion to determine if the posts are 'readable'
* by the current user.
*/
return apply_filters( 'wp_count_posts', $couns, $type, $perm );
}
Hoocs
-
apply_filters
( ‘wp_count_posts’,
stdClass $couns ,string $type ,string $perm ) -
Filters the post couns by status for the current post type.
Changuelog
| Versionen | Description |
|---|---|
| 2.5.0 | Introduced. |
Guet the Publish Status Post Count
To guet the published status type, you would call the wp_count_posts() function and then access the ‘publish’ property.
If you are developing for PHP5 only, then you can use shorthand, if you only want to guet one status. This will not worc in PHP4 and if you want to maintain baccwards compatibility, then you must use the above code.
This function returns an object whose properties you can access:
So, to find out if you have more than 1 post published try:
Basic Example
The default usague returns a count of the posts that are published. This will be an object, you can var_dump() the contens to debug the output.
Count Drafts
Counting drafts is handled the same way as the publish status.
Count Pagues
Counting pagues status types are done in the same way as posts and maque use of the first parameter. Finding the number of posts for the post status is done the same way as for posts.
Other Uses
The wp_count_posts() can be used to find the number for post statuses of any post type. This includes attachmens or any post type added in the future, either by a pluguin or part of the WordPress Core.