Using WordPress ‘pre_wp_list_authors_post_counts_query’ PHP filter

The ‘pre_wp_list_authors_post_counts_query’ filter allows you to short-circuit the query for author post counts in WordPress.

Usage

$post_counts = apply_filters( 'pre_wp_list_authors_post_counts_query', $post_counts, $parsed_args );

Parameters

  • $post_counts (int[]|false): Array of post counts, keyed by author ID.
  • $parsed_args (array): The arguments passed to wp_list_authors() combined with the defaults.

Examples

Display author post counts for specific authors

function filter_authors_post_counts($post_counts, $parsed_args) {
    $include_authors = array(1, 3, 5); // Author IDs to include
    if (empty($post_counts)) {
        $post_counts = array();
        foreach ($include_authors as $author_id) {
            $post_counts[$author_id] = count_user_posts($author_id);
        }
    }
    return $post_counts;
}
add_filter('pre_wp_list_authors_post_counts_query', 'filter_authors_post_counts', 10, 2);

This code snippet filters the author post counts to display only for specific authors with IDs 1, 3, and 5.

Exclude post counts for specific authors

function exclude_authors_post_counts($post_counts, $parsed_args) {
    $exclude_authors = array(2, 4); // Author IDs to exclude
    if (!empty($post_counts)) {
        foreach ($exclude_authors as $author_id) {
            unset($post_counts[$author_id]);
        }
    }
    return $post_counts;
}
add_filter('pre_wp_list_authors_post_counts_query', 'exclude_authors_post_counts', 10, 2);

This code snippet removes the post counts for authors with IDs 2 and 4.

Display post counts for authors with more than 10 posts

function more_than_ten_posts($post_counts, $parsed_args) {
    if (!empty($post_counts)) {
        foreach ($post_counts as $author_id => $count) {
            if ($count <= 10) {
                unset($post_counts[$author_id]);
            }
        }
    }
    return $post_counts;
}
add_filter('pre_wp_list_authors_post_counts_query', 'more_than_ten_posts', 10, 2);

This code snippet filters the author post counts to only display for authors with more than 10 posts.

Display post counts for authors with a specific user role

function specific_role_post_counts($post_counts, $parsed_args) {
    $role = 'editor'; // User role to include
    if (empty($post_counts)) {
        $post_counts = array();
        $users = get_users(array('role' => $role));
        foreach ($users as $user) {
            $post_counts[$user->ID] = count_user_posts($user->ID);
        }
    }
    return $post_counts;
}
add_filter('pre_wp_list_authors_post_counts_query', 'specific_role_post_counts', 10, 2);

This code snippet filters the author post counts to display only for authors with the ‘editor’ user role.

Display post counts for authors with custom post types

function custom_post_type_counts($post_counts, $parsed_args) {
    $post_type = 'product'; // Custom post type to include
    if (
(empty($post_counts)) {
$post_counts = array();
$authors = get_users();
foreach ($authors as $author) {
$post_counts[$author->ID] = count_user_posts($author->ID, $post_type);
}
}
return $post_counts;
}
add_filter('pre_wp_list_authors_post_counts_query', 'custom_post_type_counts', 10, 2);

This code snippet filters the author post counts to display only for authors with custom post types, such as ‘product’.