wp_count_attachments

函式
wp_count_attachments ( $mime_type = '' )
引數
  • (string|string[]) $mime_type Optional. Array or comma-separated list of MIME patterns. Default empty.
    Required:
    Default: (empty)
返回值
  • (stdClass) An object containing the attachment counts by mime type.
定義位置
相關方法
wp_count_commentswp_insert_attachmentwp_attachment_iswp_delete_attachmentwp_get_attachment_url
引入
2.5.0
棄用
-

wp_count_attachments: 這是一個返回媒體庫中附件數量的函式。它可以用來獲得媒體庫的概況,並跟蹤附件的數量。

計算mime型別的附件數量。

如果你設定了可選的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 );
}

常見問題

FAQs
檢視更多 >