函数文档

count_many_users_posts()

💡 云策文档标注

概述

count_many_users_posts() 是一个 WordPress 函数,用于获取指定用户列表的文章数量。它支持自定义文章类型和公开文章筛选,并包含缓存机制以提高性能。

关键要点

  • 函数接受用户 ID 数组、文章类型参数和公开文章标志作为输入,返回以用户 ID 为键的文章数量数组。
  • 内部使用 SQL 查询和缓存系统(如 wp_cache_get_salted)来优化性能,避免重复数据库查询。
  • 提供 pre_count_many_users_posts 过滤器,允许开发者短路默认计数逻辑,自定义返回结果。
  • 函数在 6.9.0 版本引入了缓存功能,增强了处理效率。

代码示例

$users = array( 1, 3, 9, 10 );
$counts = count_many_users_posts( $users );
echo 'Posts made by user 3: ' . $counts[3];

注意事项

  • 确保传入的用户 ID 数组有效且非空,否则函数返回空数组。
  • 文章类型参数默认为 'post',可传入字符串或数组以支持多种类型。
  • 缓存机制依赖于 posts 和 users 缓存组的变化,确保相关数据更新时缓存能正确失效。

📄 原文内容

Gets the number of posts written by a list of users.

Parameters

$usersint[]required
Array of user IDs.
$post_typestring|string[]optional
Single post type or array of post types to check. Defaults to 'post'.
$public_onlybooloptional
Only return counts for public posts. Defaults to false.

Default:false

Return

array<int, string> Amount of posts each user has written, as strings, keyed by user ID.

Source

function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
	global $wpdb;

	if ( empty( $users ) || ! is_array( $users ) ) {
		return array();
	}

	/**
	 * Filters whether to short-circuit performing the post counts.
	 *
	 * When filtering, return an array of posts counts as strings, keyed
	 * by the user ID.
	 *
	 * @since 6.8.0
	 *
	 * @param string[]|null   $count       The post counts. Return a non-null value to short-circuit.
	 * @param int[]           $users       Array of user IDs.
	 * @param string|string[] $post_type   Single post type or array of post types to check.
	 * @param bool            $public_only Whether to only return counts for public posts.
	 */
	$pre = apply_filters( 'pre_count_many_users_posts', null, $users, $post_type, $public_only );
	if ( null !== $pre ) {
		return $pre;
	}

	// Cleanup the users array. Remove duplicates and sort for consistent ordering.
	$users = array_unique( array_filter( array_map( 'intval', $users ) ) );
	sort( $users );

	// Cleanup the post type argument. Remove duplicates and sort for consistent ordering.
	$post_type = array_unique( (array) $post_type );
	sort( $post_type );

	$userlist    = implode( ',', $users );
	$where       = get_posts_by_author_sql( $post_type, true, null, $public_only );
	$query       = "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author";
	$cache_key   = 'count_many_users_posts:' . md5( $query );
	$cache_salts = array( wp_cache_get_last_changed( 'posts' ), wp_cache_get_last_changed( 'users' ) );
	$count       = wp_cache_get_salted( $cache_key, 'post-queries', $cache_salts );

	if ( false === $count ) {
		$result = $wpdb->get_results( $query, ARRAY_N );

		$count = array_fill_keys( $users, 0 );
		foreach ( $result as $row ) {
			$count[ $row[0] ] = $row[1];
		}

		wp_cache_set_salted( $cache_key, $count, 'post-queries', $cache_salts, HOUR_IN_SECONDS );
	}

	return $count;
}

Hooks

apply_filters( ‘pre_count_many_users_posts’, string[]|null $count, int[] $users, string|string[] $post_type, bool $public_only )

Filters whether to short-circuit performing the post counts.

Changelog

Version Description
6.9.0 The results are now cached.
3.0.0 Introduced.

User Contributed Notes