Custom Post Type Counts in the Users List
Adds a column per custom post type to the Users screen showing how many published items each user has, next to the built-in Posts count.
Opens WPIDE on
Code
// Post types to count. Leave empty to use every public custom post type.
$wpide_user_count_types = array();
$wpide_user_count_types_resolved = function () use ( $wpide_user_count_types ) {
return $wpide_user_count_types ? $wpide_user_count_types : array_values( get_post_types( array( 'public' => true, '_builtin' => false ) ) );
};
add_filter( 'manage_users_columns', function ( $columns ) use ( $wpide_user_count_types_resolved ) {
foreach ( $wpide_user_count_types_resolved() as $type ) {
$object = get_post_type_object( $type );
if ( $object ) {
$columns[ 'wpide_count_' . $type ] = esc_html( $object->labels->name );
}
}
return $columns;
} );
add_filter( 'manage_users_custom_column', function ( $output, $column, $user_id ) {
if ( 0 !== strpos( $column, 'wpide_count_' ) ) {
return $output;
}
$type = substr( $column, strlen( 'wpide_count_' ) );
$count = (int) count_user_posts( $user_id, $type, true );
if ( ! $count ) {
return '0';
}
return sprintf( '<a href="%s">%d</a>', esc_url( admin_url( 'edit.php?post_type=' . $type . '&author=' . $user_id ) ), $count );
}, 10, 3 );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.