Using WordPress ‘get_usernumposts’ PHP filter

The get_usernumposts WordPress PHP filter allows you to modify the number of posts a user has written.

Usage

add_filter('get_usernumposts', 'your_custom_function', 10, 4);

function your_custom_function($count, $userid, $post_type, $public_only) {
    // your custom code here
    return $count;
}

Parameters

  • $count (int) – The user’s post count.
  • $userid (int) – User ID.
  • $post_type (string|array) – Single post type or array of post types to count the number of posts for.
  • $public_only (bool) – Whether to limit counted posts to public posts.

More information

See WordPress Developer Resources: get_usernumposts

Examples

Exclude specific post types

Exclude ‘attachment’ and ‘revision’ post types from the user’s post count.

function exclude_post_types_from_count($count, $userid, $post_type, $public_only) {
    $excluded_post_types = array('attachment', 'revision');
    $user_posts = get_posts(array(
        'author'      => $userid,
        'post_type'   => $post_type,
        'post_status' => 'any',
        'numberposts' => -1,
        'exclude'     => $excluded_post_types
    ));
    return count($user_posts);
}
add_filter('get_usernumposts', 'exclude_post_types_from_count', 10, 4);

Count only published posts

Count only published posts for a user.

function count_published_posts_only($count, $userid, $post_type, $public_only) {
    $user_posts = get_posts(array(
        'author'      => $userid,
        'post_type'   => $post_type,
        'post_status' => 'publish',
        'numberposts' => -1
    ));
    return count($user_posts);
}
add_filter('get_usernumposts', 'count_published_posts_only', 10, 4);

Increase post count by a fixed number

Increase the user’s post count by 10.

function increase_post_count($count, $userid, $post_type, $public_only) {
    $count += 10;
    return $count;
}
add_filter('get_usernumposts', 'increase_post_count', 10, 4);

Count posts for a specific post type

Count only ‘product’ post type for a user.

function count_product_post_type($count, $userid, $post_type, $public_only) {
    $user_posts = get_posts(array(
        'author'      => $userid,
        'post_type'   => 'product',
        'post_status' => 'any',
        'numberposts' => -1
    ));
    return count($user_posts);
}
add_filter('get_usernumposts', 'count_product_post_type', 10, 4);

Count posts in a specific category

Count user’s posts in a specific category (e.g., category ID 5).

function count_posts_in_category($count, $userid, $post_type, $public_only) {
    $user_posts = get_posts(array(
        'author'      => $userid,
        'post_type'   => $post_type,
'post_status' => 'any', 
'numberposts' => -1, 
'category' => 5 
)
); 
return count($user_posts); 
} 
add_filter('get_usernumposts', 'count_posts_in_category', 10, 4);

‘post_status’ => ‘any’,
‘numberposts’ => -1,
‘category’ => 5
));
return count($user_posts);
}
add_filter(‘get_usernumposts’, ‘count_posts_in_category’, 10, 4);

Double the user’s post count

Double the user’s post count.

function double_post_count($count, $userid, $post_type, $public_only) {
    $count *= 2;
    return $count;
}
add_filter('get_usernumposts', 'double_post_count', 10, 4);

Count only posts with a specific meta key

Count user’s posts that have the meta key ‘featured’.

function count_posts_with_meta_key($count, $userid, $post_type, $public_only) {
    $user_posts = get_posts(array(
        'author'      => $userid,
        'post_type'   => $post_type,
        'post_status' => 'any',
        'numberposts' => -1,
        'meta_key'    => 'featured'
    ));
    return count($user_posts);
}
add_filter('get_usernumposts', 'count_posts_with_meta_key', 10, 4);