Languages: English • Italiano • (Add your language)
Returns the post count for a user.
<?php $user_post_count = count_user_posts( $userid , $post_type ); ?>
Display the number of posts published by the user with an ID of 5.
<?php echo 'Number of posts published by user: ' . count_user_posts( 5 ); ?>
Display the number of posts of post type "book" published by the user with an ID of 5.
<?php echo 'Number of posts published by user: ' . count_user_posts( 5 , "book" ); ?>
The same operation, with translation support.
<?php printf( __( 'Number of posts published by user: %d', 'text-dom-here' ), count_user_posts( 5 ) ); ?>
The result of the above two examples
Note : Since 4.1 it supports post types.
As post_type support is not currently available for count_user_posts(), below you will find a derivative function with post_type support provided via a secondary parameter.
<?php function count_user_posts_by_type( $userid, $post_type = 'post' ) { global $wpdb; $where = get_posts_by_author_sql( $post_type, true, $userid ); $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ); return apply_filters( 'get_usernumposts', $count, $userid ); } ?>
count_user_posts() is located in wp-includes/user.php
.