函数文档

wp_count_attachments()

💡 云策文档标注

概述

wp_count_attachments() 函数用于统计 WordPress 中附件的数量,可按 MIME 类型筛选。它返回一个包含各 MIME 类型附件计数的对象,并支持缓存以提高性能。

关键要点

  • 函数统计附件数量,可选参数 $mime_type 用于指定 MIME 类型或模式列表进行筛选。
  • 返回值为 stdClass 对象,键为 MIME 类型,值为对应附件数量,包括 'trash' 键表示已删除附件。
  • 内部使用缓存机制(wp_cache_get/set)优化查询性能,避免重复数据库操作。
  • 通过 apply_filters('wp_count_attachments', ...) 提供过滤器,允许开发者修改返回的计数数据。
  • 注意:该函数不统计作为文章子项的附件数量,需通过其他方式获取。

代码示例

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' );
    }

    return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type );
}

注意事项

  • 参数 $mime_type 可为字符串或数组,用于过滤 MIME 类型,默认空值返回所有类型。
  • 函数不处理作为文章子项的附件,需通过其他查询获取子项数量。
  • 返回对象包含 'trash' 键,表示已移至回收站的附件数量。
  • 使用缓存键基于 MIME 类型生成,确保不同查询的缓存隔离。

📄 原文内容

Counts number of attachments for the mime type(s).

Description

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.

Parameters

$mime_typestring|string[]optional
Array or comma-separated list of MIME patterns. Default empty.

Return

stdClass An object containing the attachment counts by mime type.

Source

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 );
}

Hooks

apply_filters( ‘wp_count_attachments’, stdClass $counts, string|string[] $mime_type )

Filters the attachment counts by mime type.

Changelog

Version Description
2.5.0 Introduced.