计算mime类型的附件数量。

原型

wp_count_attachments( string|array $mime_type = '' )

描述

如果设置了可选的mime_type参数,那么仍会返回一个数组,但只会包含你要查找的项目。它不会为你提供帖子子项的附件数量。你可以通过计算发布的孩子数来获得。

参数

$mime_type (string|array) (Optional)  数组或逗号分隔的MIME模式列表。

返回值

(object)  包含附件的对象按mime类型计数。

源文件

路径:wp-includes/post.php

<?php
...
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 );
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_count_attachments/