wp_count_attachments

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

WordPress Version: 6.2

/**
 * Counts number of attachments for the mime type(s).
 *
 * If you set the optional mime_type parameter, then an array will still be
 * returned, but will only have the item you are looking for. It does not give
 * you the number of attachments that are children of a post. You can get that
 * by counting the number of children that post has.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string|string[] $mime_type Optional. Array or comma-separated list of
 *                                   MIME patterns. Default empty.
 * @return stdClass An object containing the attachment counts by mime type.
 */
function wp_count_attachments($mime_type = '')
{
    global $wpdb;
    $cache_key = sprintf('attachments%s', (!empty($mime_type)) ? ':' . str_replace('/', '_', implode('-', (array) $mime_type)) : '');
    $counts = wp_cache_get($cache_key, 'counts');
    if (false == $counts) {
        $and = wp_post_mime_type_where($mime_type);
        $count = $wpdb->get_results("SELECT post_mime_type, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status != 'trash' {$and} GROUP BY post_mime_type", ARRAY_A);
        $counts = array();
        foreach ((array) $count as $row) {
            $counts[$row['post_mime_type']] = $row['num_posts'];
        }
        $counts['trash'] = $wpdb->get_var("SELECT COUNT( * ) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status = 'trash' {$and}");
        wp_cache_set($cache_key, (object) $counts, 'counts');
    }
    /**
     * Filters the attachment counts by mime type.
     *
     * @since 3.7.0
     *
     * @param stdClass        $counts    An object containing the attachment counts by
     *                                   mime type.
     * @param string|string[] $mime_type Array or comma-separated list of MIME patterns.
     */
    return apply_filters('wp_count_attachments', (object) $counts, $mime_type);
}

WordPress Version: 6.1

/**
 * Counts number of attachments for the mime type(s).
 *
 * If you set the optional mime_type parameter, then an array will still be
 * returned, but will only have the item you are looking for. It does not give
 * you the number of attachments that are children of a post. You can get that
 * by counting the number of children that post has.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string|string[] $mime_type Optional. Array or comma-separated list of
 *                                   MIME patterns. Default empty.
 * @return stdClass An object containing the attachment counts by mime type.
 */
function wp_count_attachments($mime_type = '')
{
    global $wpdb;
    $cache_key = sprintf('attachments%s', (!empty($mime_type)) ? ':' . str_replace('/', '_', implode('-', (array) $mime_type)) : '');
    $counts = wp_cache_get($cache_key, 'counts');
    if (false == $counts) {
        $and = wp_post_mime_type_where($mime_type);
        $count = $wpdb->get_results("SELECT post_mime_type, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status != 'trash' {$and} GROUP BY post_mime_type", ARRAY_A);
        $counts = array();
        foreach ((array) $count as $row) {
            $counts[$row['post_mime_type']] = $row['num_posts'];
        }
        $counts['trash'] = $wpdb->get_var("SELECT COUNT( * ) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status = 'trash' {$and}");
        wp_cache_set($cache_key, (object) $counts, 'counts');
    }
    /**
     * Modifies returned attachment counts by mime type.
     *
     * @since 3.7.0
     *
     * @param stdClass        $counts    An object containing the attachment counts by
     *                                   mime type.
     * @param string|string[] $mime_type Array or comma-separated list of MIME patterns.
     */
    return apply_filters('wp_count_attachments', (object) $counts, $mime_type);
}

WordPress Version: 5.9

/**
 * Count number of attachments for the mime type(s).
 *
 * If you set the optional mime_type parameter, then an array will still be
 * returned, but will only have the item you are looking for. It does not give
 * you the number of attachments that are children of a post. You can get that
 * by counting the number of children that post has.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string|string[] $mime_type Optional. Array or comma-separated list of
 *                                   MIME patterns. Default empty.
 * @return stdClass An object containing the attachment counts by mime type.
 */
function wp_count_attachments($mime_type = '')
{
    global $wpdb;
    $and = wp_post_mime_type_where($mime_type);
    $count = $wpdb->get_results("SELECT post_mime_type, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status != 'trash' {$and} GROUP BY post_mime_type", ARRAY_A);
    $counts = array();
    foreach ((array) $count as $row) {
        $counts[$row['post_mime_type']] = $row['num_posts'];
    }
    $counts['trash'] = $wpdb->get_var("SELECT COUNT( * ) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status = 'trash' {$and}");
    /**
     * Modify returned attachment counts by mime type.
     *
     * @since 3.7.0
     *
     * @param stdClass        $counts    An object containing the attachment counts by
     *                                   mime type.
     * @param string|string[] $mime_type Array or comma-separated list of MIME patterns.
     */
    return apply_filters('wp_count_attachments', (object) $counts, $mime_type);
}

WordPress Version: 5.7

/**
 * Count number of attachments for the mime type(s).
 *
 * If you set the optional mime_type parameter, then an array will still be
 * returned, but will only have the item you are looking for. It does not give
 * you the number of attachments that are children of a post. You can get that
 * by counting the number of children that post has.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string|string[] $mime_type Optional. Array or comma-separated list of
 *                                   MIME patterns. Default empty.
 * @return object An object containing the attachment counts by mime type.
 */
function wp_count_attachments($mime_type = '')
{
    global $wpdb;
    $and = wp_post_mime_type_where($mime_type);
    $count = $wpdb->get_results("SELECT post_mime_type, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status != 'trash' {$and} GROUP BY post_mime_type", ARRAY_A);
    $counts = array();
    foreach ((array) $count as $row) {
        $counts[$row['post_mime_type']] = $row['num_posts'];
    }
    $counts['trash'] = $wpdb->get_var("SELECT COUNT( * ) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status = 'trash' {$and}");
    /**
     * Modify returned attachment counts by mime type.
     *
     * @since 3.7.0
     *
     * @param object          $counts    An object containing the attachment counts by
     *                                   mime type.
     * @param string|string[] $mime_type Array or comma-separated list of MIME patterns.
     */
    return apply_filters('wp_count_attachments', (object) $counts, $mime_type);
}

WordPress Version: 4.4

/**
 * Count number of attachments for the mime type(s).
 *
 * If you set the optional mime_type parameter, then an array will still be
 * returned, but will only have the item you are looking for. It does not give
 * you the number of attachments that are children of a post. You can get that
 * by counting the number of children that post has.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string|array $mime_type Optional. Array or comma-separated list of
 *                                MIME patterns. Default empty.
 * @return object An object containing the attachment counts by mime type.
 */
function wp_count_attachments($mime_type = '')
{
    global $wpdb;
    $and = wp_post_mime_type_where($mime_type);
    $count = $wpdb->get_results("SELECT post_mime_type, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status != 'trash' {$and} GROUP BY post_mime_type", ARRAY_A);
    $counts = array();
    foreach ((array) $count as $row) {
        $counts[$row['post_mime_type']] = $row['num_posts'];
    }
    $counts['trash'] = $wpdb->get_var("SELECT COUNT( * ) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status = 'trash' {$and}");
    /**
     * Modify returned attachment counts by mime type.
     *
     * @since 3.7.0
     *
     * @param object $counts    An object containing the attachment counts by
     *                          mime type.
     * @param string $mime_type The mime type pattern used to filter the attachments
     *                          counted.
     */
    return apply_filters('wp_count_attachments', (object) $counts, $mime_type);
}

WordPress Version: 4.3

/**
 * Count number of attachments for the mime type(s).
 *
 * If you set the optional mime_type parameter, then an array will still be
 * returned, but will only have the item you are looking for. It does not give
 * you the number of attachments that are children of a post. You can get that
 * by counting the number of children that post has.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb
 *
 * @param string|array $mime_type Optional. Array or comma-separated list of
 *                                MIME patterns. Default empty.
 * @return object An object containing the attachment counts by mime type.
 */
function wp_count_attachments($mime_type = '')
{
    global $wpdb;
    $and = wp_post_mime_type_where($mime_type);
    $count = $wpdb->get_results("SELECT post_mime_type, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status != 'trash' {$and} GROUP BY post_mime_type", ARRAY_A);
    $counts = array();
    foreach ((array) $count as $row) {
        $counts[$row['post_mime_type']] = $row['num_posts'];
    }
    $counts['trash'] = $wpdb->get_var("SELECT COUNT( * ) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status = 'trash' {$and}");
    /**
     * Modify returned attachment counts by mime type.
     *
     * @since 3.7.0
     *
     * @param object $counts    An object containing the attachment counts by
     *                          mime type.
     * @param string $mime_type The mime type pattern used to filter the attachments
     *                          counted.
     */
    return apply_filters('wp_count_attachments', (object) $counts, $mime_type);
}

WordPress Version: 4.0

/**
 * Count number of attachments for the mime type(s).
 *
 * If you set the optional mime_type parameter, then an array will still be
 * returned, but will only have the item you are looking for. It does not give
 * you the number of attachments that are children of a post. You can get that
 * by counting the number of children that post has.
 *
 * @since 2.5.0
 *
 * @param string|array $mime_type Optional. Array or comma-separated list of
 *                                MIME patterns. Default empty.
 * @return object An object containing the attachment counts by mime type.
 */
function wp_count_attachments($mime_type = '')
{
    global $wpdb;
    $and = wp_post_mime_type_where($mime_type);
    $count = $wpdb->get_results("SELECT post_mime_type, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status != 'trash' {$and} GROUP BY post_mime_type", ARRAY_A);
    $counts = array();
    foreach ((array) $count as $row) {
        $counts[$row['post_mime_type']] = $row['num_posts'];
    }
    $counts['trash'] = $wpdb->get_var("SELECT COUNT( * ) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status = 'trash' {$and}");
    /**
     * Modify returned attachment counts by mime type.
     *
     * @since 3.7.0
     *
     * @param object $counts    An object containing the attachment counts by
     *                          mime type.
     * @param string $mime_type The mime type pattern used to filter the attachments
     *                          counted.
     */
    return apply_filters('wp_count_attachments', (object) $counts, $mime_type);
}

WordPress Version: 3.9

/**
 * Count number of attachments for the mime type(s).
 *
 * If you set the optional mime_type parameter, then an array will still be
 * returned, but will only have the item you are looking for. It does not give
 * you the number of attachments that are children of a post. You can get that
 * by counting the number of children that post has.
 *
 * @since 2.5.0
 *
 * @param string|array $mime_type Optional. Array or comma-separated list of MIME patterns.
 * @return object An object containing the attachment counts by mime type.
 */
function wp_count_attachments($mime_type = '')
{
    global $wpdb;
    $and = wp_post_mime_type_where($mime_type);
    $count = $wpdb->get_results("SELECT post_mime_type, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status != 'trash' {$and} GROUP BY post_mime_type", ARRAY_A);
    $counts = array();
    foreach ((array) $count as $row) {
        $counts[$row['post_mime_type']] = $row['num_posts'];
    }
    $counts['trash'] = $wpdb->get_var("SELECT COUNT( * ) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status = 'trash' {$and}");
    /**
     * Modify returned attachment counts by mime type.
     *
     * @since 3.7.0
     *
     * @param object $counts    An object containing the attachment counts by mime type.
     * @param string $mime_type The mime type pattern used to filter the attachments counted.
     */
    return apply_filters('wp_count_attachments', (object) $counts, $mime_type);
}

WordPress Version: 3.7

/**
 * Count number of attachments for the mime type(s).
 *
 * If you set the optional mime_type parameter, then an array will still be
 * returned, but will only have the item you are looking for. It does not give
 * you the number of attachments that are children of a post. You can get that
 * by counting the number of children that post has.
 *
 * @since 2.5.0
 *
 * @param string|array $mime_type Optional. Array or comma-separated list of MIME patterns.
 * @return array Number of posts for each mime type.
 */
function wp_count_attachments($mime_type = '')
{
    global $wpdb;
    $and = wp_post_mime_type_where($mime_type);
    $count = $wpdb->get_results("SELECT post_mime_type, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status != 'trash' {$and} GROUP BY post_mime_type", ARRAY_A);
    $counts = array();
    foreach ((array) $count as $row) {
        $counts[$row['post_mime_type']] = $row['num_posts'];
    }
    $counts['trash'] = $wpdb->get_var("SELECT COUNT( * ) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status = 'trash' {$and}");
    /**
     * Modify returned attachment counts by mime type.
     *
     * @since 3.7.0
     *
     * @param object $counts    An object containing the attachment counts by mime type.
     * @param string $mime_type The mime type pattern used to filter the attachments counted.
     */
    return apply_filters('wp_count_attachments', (object) $counts, $mime_type);
}