count_user_posts

The timeline below displays how wordpress function count_user_posts has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Gets the number of posts a user has written.
 *
 * @since 3.0.0
 * @since 4.1.0 Added `$post_type` argument.
 * @since 4.3.0 Added `$public_only` argument. Added the ability to pass an array
 *              of post types to `$post_type`.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int          $userid      User ID.
 * @param array|string $post_type   Optional. Single post type or array of post types to count the number of posts for. Default 'post'.
 * @param bool         $public_only Optional. Whether to only return counts for public posts. Default false.
 * @return string Number of posts the user has written in this post type.
 */
function count_user_posts($userid, $post_type = 'post', $public_only = false)
{
    global $wpdb;
    $where = get_posts_by_author_sql($post_type, true, $userid, $public_only);
    $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}");
    /**
     * Filters the number of posts a user has written.
     *
     * @since 2.7.0
     * @since 4.1.0 Added `$post_type` argument.
     * @since 4.3.1 Added `$public_only` argument.
     *
     * @param int          $count       The user's post count.
     * @param int          $userid      User ID.
     * @param string|array $post_type   Single post type or array of post types to count the number of posts for.
     * @param bool         $public_only Whether to limit counted posts to public posts.
     */
    return apply_filters('get_usernumposts', $count, $userid, $post_type, $public_only);
}

WordPress Version: 4.6

/**
 * Number of posts user has written.
 *
 * @since 3.0.0
 * @since 4.1.0 Added `$post_type` argument.
 * @since 4.3.0 Added `$public_only` argument. Added the ability to pass an array
 *              of post types to `$post_type`.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int          $userid      User ID.
 * @param array|string $post_type   Optional. Single post type or array of post types to count the number of posts for. Default 'post'.
 * @param bool         $public_only Optional. Whether to only return counts for public posts. Default false.
 * @return string Number of posts the user has written in this post type.
 */
function count_user_posts($userid, $post_type = 'post', $public_only = false)
{
    global $wpdb;
    $where = get_posts_by_author_sql($post_type, true, $userid, $public_only);
    $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}");
    /**
     * Filters the number of posts a user has written.
     *
     * @since 2.7.0
     * @since 4.1.0 Added `$post_type` argument.
     * @since 4.3.1 Added `$public_only` argument.
     *
     * @param int          $count       The user's post count.
     * @param int          $userid      User ID.
     * @param string|array $post_type   Single post type or array of post types to count the number of posts for.
     * @param bool         $public_only Whether to limit counted posts to public posts.
     */
    return apply_filters('get_usernumposts', $count, $userid, $post_type, $public_only);
}

WordPress Version: 4.5

/**
 * Number of posts user has written.
 *
 * @since 3.0.0
 * @since 4.1.0 Added `$post_type` argument.
 * @since 4.3.0 Added `$public_only` argument. Added the ability to pass an array
 *              of post types to `$post_type`.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int          $userid      User ID.
 * @param array|string $post_type   Optional. Single post type or array of post types to count the number of posts for. Default 'post'.
 * @param bool         $public_only Optional. Whether to only return counts for public posts. Default false.
 * @return string Number of posts the user has written in this post type.
 */
function count_user_posts($userid, $post_type = 'post', $public_only = false)
{
    global $wpdb;
    $where = get_posts_by_author_sql($post_type, true, $userid, $public_only);
    $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}");
    /**
     * Filter the number of posts a user has written.
     *
     * @since 2.7.0
     * @since 4.1.0 Added `$post_type` argument.
     * @since 4.3.1 Added `$public_only` argument.
     *
     * @param int          $count       The user's post count.
     * @param int          $userid      User ID.
     * @param string|array $post_type   Single post type or array of post types to count the number of posts for.
     * @param bool         $public_only Whether to limit counted posts to public posts.
     */
    return apply_filters('get_usernumposts', $count, $userid, $post_type, $public_only);
}

WordPress Version: 4.4

/**
 * Number of posts user has written.
 *
 * @since 3.0.0
 * @since 4.1.0 Added `$post_type` argument.
 * @since 4.3.0 Added `$public_only` argument. Added the ability to pass an array
 *              of post types to `$post_type`.
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int          $userid      User ID.
 * @param array|string $post_type   Optional. Single post type or array of post types to count the number of posts for. Default 'post'.
 * @param bool         $public_only Optional. Whether to only return counts for public posts. Default false.
 * @return int Number of posts the user has written in this post type.
 */
function count_user_posts($userid, $post_type = 'post', $public_only = false)
{
    global $wpdb;
    $where = get_posts_by_author_sql($post_type, true, $userid, $public_only);
    $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}");
    /**
     * Filter the number of posts a user has written.
     *
     * @since 2.7.0
     * @since 4.1.0 Added `$post_type` argument.
     * @since 4.3.1 Added `$public_only` argument.
     *
     * @param int          $count       The user's post count.
     * @param int          $userid      User ID.
     * @param string|array $post_type   Single post type or array of post types to count the number of posts for.
     * @param bool         $public_only Whether to limit counted posts to public posts.
     */
    return apply_filters('get_usernumposts', $count, $userid, $post_type, $public_only);
}

WordPress Version: 3.1

/**
 * Number of posts user has written.
 *
 * @since 3.0.0
 * @since 4.1.0 Added `$post_type` argument.
 * @since 4.3.0 Added `$public_only` argument. Added the ability to pass an array
 *              of post types to `$post_type`.
 *
 * @global wpdb $wpdb WordPress database object for queries.
 *
 * @param int          $userid      User ID.
 * @param array|string $post_type   Optional. Single post type or array of post types to count the number of posts for. Default 'post'.
 * @param bool         $public_only Optional. Whether to only return counts for public posts. Default false.
 * @return int Number of posts the user has written in this post type.
 */
function count_user_posts($userid, $post_type = 'post', $public_only = false)
{
    global $wpdb;
    $where = get_posts_by_author_sql($post_type, true, $userid, $public_only);
    $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}");
    /**
     * Filter the number of posts a user has written.
     *
     * @since 2.7.0
     * @since 4.1.0 Added `$post_type` argument.
     * @since 4.3.1 Added `$public_only` argument.
     *
     * @param int          $count       The user's post count.
     * @param int          $userid      User ID.
     * @param string|array $post_type   Single post type or array of post types to count the number of posts for.
     * @param bool         $public_only Whether to limit counted posts to public posts.
     */
    return apply_filters('get_usernumposts', $count, $userid, $post_type, $public_only);
}

WordPress Version: 4.3

/**
 * Number of posts user has written.
 *
 * @since 3.0.0
 * @since 4.1.0 Added `$post_type` argument.
 * @since 4.3.0 Added `$public_only` argument. Added the ability to pass an array
 *              of post types to `$post_type`.
 *
 * @global wpdb $wpdb WordPress database object for queries.
 *
 * @param int          $userid      User ID.
 * @param array|string $post_type   Optional. Post type(s) to count the number of posts for. Default 'post'.
 * @param bool         $public_only Optional. Whether to only return counts for public posts. Default false.
 * @return int Number of posts the user has written in this post type.
 */
function count_user_posts($userid, $post_type = 'post', $public_only = false)
{
    global $wpdb;
    $where = get_posts_by_author_sql($post_type, true, $userid, $public_only);
    $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}");
    /**
     * Filter the number of posts a user has written.
     *
     * @since 2.7.0
     * @since 4.1.0 Added `$post_type` argument.
     * @since 4.3.0 Added `$public_only` argument.
     *
     * @param int          $count       The user's post count.
     * @param int          $userid      User ID.
     * @param string|array $post_types  Post types to count the number of posts for.
     * @param bool         $public_only Whether to limit counted posts to public posts.
     */
    return apply_filters('get_usernumposts', $count, $userid, $post_type);
}

WordPress Version: 4.1

/**
 * Number of posts user has written.
 *
 * @since 3.0.0
 * @since 4.1.0 Added `$post_type` argument.
 *
 * @global wpdb $wpdb WordPress database object for queries.
 *
 * @param int    $userid    User ID.
 * @param string $post_type Optional. Post type to count the number of posts for. Default 'post'.
 * @return int Number of posts the user has written in this post type.
 */
function count_user_posts($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}");
    /**
     * Filter the number of posts a user has written.
     *
     * @since 2.7.0
     * @since 4.1.0 Added `$post_type` argument.
     *
     * @param int    $count     The user's post count.
     * @param int    $userid    User ID.
     * @param string $post_type Post type to count the number of posts for.
     */
    return apply_filters('get_usernumposts', $count, $userid, $post_type);
}

WordPress Version: 3.9

/**
 * Number of posts user has written.
 *
 * @since 3.0.0
 *
 * @global wpdb $wpdb WordPress database object for queries.
 *
 * @param int $userid User ID.
 * @return int Amount of posts user has written.
 */
function count_user_posts($userid)
{
    global $wpdb;
    $where = get_posts_by_author_sql('post', true, $userid);
    $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}");
    /**
     * Filter the number of posts a user has written.
     *
     * @since 2.7.0
     *
     * @param int $count  The user's post count.
     * @param int $userid User ID.
     */
    return apply_filters('get_usernumposts', $count, $userid);
}

WordPress Version: 3.7

/**
 * Number of posts user has written.
 *
 * @since 3.0.0
 * @uses $wpdb WordPress database object for queries.
 *
 * @param int $userid User ID.
 * @return int Amount of posts user has written.
 */
function count_user_posts($userid)
{
    global $wpdb;
    $where = get_posts_by_author_sql('post', true, $userid);
    $count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} {$where}");
    return apply_filters('get_usernumposts', $count, $userid);
}