返回介绍

wp_count_attachments()

发布于 2017-09-11 11:43:20 字数 2664 浏览 975 评论 0 收藏 0

wp_count_attachments( string|array $mime_type = '' )

Count 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.


参数

$mime_type

(string|array) (Optional) Array or comma-separated list of MIME patterns.

Default value: ''


返回值

(object) An object containing the attachment counts by mime type.


源代码

File: wp-includes/post.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 );
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post.php: wp_post_mime_type_where()
  • wp-includes/post.php: wp_count_attachments
  • wp-includes/wp-db.php: wpdb::get_results()
  • wp-includes/wp-db.php: wpdb::get_var()

Used By

  • wp-admin/includes/media.php: media_upload_library_form()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文