Post count for users by custom post type
This function shows the users post\CPT count for each custom post type – similar to the dashboard box ‘at a glance’. You can see it at USERS -> ALL USERS. Instead of the default postcount there are now all custom post types listed and the user’s count for each of it.
/** * Snippet Name: Post count for users by custom post type * Snippet URL: https://wpcustoms.net/snippets/post-count-users-custom-post-type/ */ // unset the default posts table and create a new table "user_contributions" function wpc_manage_users_columns($column_headers) { unset($column_headers['posts']); $column_headers['user_contributions'] = 'User Posts'; return $column_headers; } add_action('manage_users_columns', 'wpc_manage_users_columns'); // call our function for non-standard columns and connect our counting function 'wpc_get_author_post_type_counts' add_action('manage_users_custom_column', 'wpc_manage_users_custom_column', 10, 3); function wpc_manage_users_custom_column($custom_column, $column_name, $user_id) { if ($column_name == 'user_contributions') { $counts = wpc_get_author_post_type_counts(); $custom_column = array(); if (isset($counts[$user_id]) && is_array($counts[$user_id])) foreach($counts[$user_id] as $count) $custom_column[] = "\t"; $custom_column = implode("\n", $custom_column); } if (empty($custom_column)) $custom_column = "No contribution yet."; else $custom_column = " {$count['label']} " . "{$count['count']}