The default version of the WordPress function provides the count of the default post type ‘POST’ as stated below:
<?php
$post_count = count_user_posts($userid);
?>
For retrieving the post count based on a post type a custom function as below is required:
<?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 $count;
}
?>
Usage of the above function:
<?php
$post_count = count_user_posts_by_type($userid, $post_type);
?>
Now as you have got the post count wrap your code in a conditional loop.
How to use in your function:
<?php
// count number of Articles (CPT) written by a user
function count_user_posts_by_type( $userid, $post_type="publication" ) {
global $wpdb;
$where = get_posts_by_author_sql( $post_type, true, $userid );
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
return $count;
}
$allUsers = get_users('orderby=title&order=ASC&post_type=publication');
$users = array();
// Remove subscribers from the list as they won't write any articles
foreach($allUsers as $currentUser)
{
if(!in_array( 'subscriber', $currentUser->roles ))
{
$users[] = $currentUser;
}
}
foreach($users as $user) {
$post_count = count_user_posts_by_type($user->id);
if ($post_count > 0 { ?>
<div class="authorInfo">
<h2 class="authorName"><?php echo $user->display_name; ?></h2>
<?php query_posts( array( 'post_type' => 'publication', 'showposts' => -1, 'author'=>$user->ID ) );
while (have_posts()) : the_post();
the_content();
endwhile; ?>
</div>
<?php }
}
?>