count_many_users_posts

函式
count_many_users_posts ( $users, $post_type = 'post', $public_only = false )
引數
  • (int[]) $users Array of user IDs.
    Required:
  • (string|string[]) $post_type Optional. Single post type or array of post types to check. Defaults to 'post'.
    Required:
    Default: 'post'
  • (bool) $public_only Optional. Only return counts for public posts. Defaults to false.
    Required:
    Default: false
返回值
  • (string[]) Amount of posts each user has written, as strings, keyed by user ID.
定義位置
相關方法
count_user_postscount_userswp_count_postsget_usernumpostsget_all_user_settings
引入
3.0.0
棄用
-

WordPress中的count_many_users_posts函式是用來計算多個使用者的文章數量的: 這個函式接收一個使用者ID的陣列作為引數,並返回每個使用者的文章數的陣列。

獲得一個使用者列表中的文章數量。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
global $wpdb;
$count = array();
if ( empty( $users ) || ! is_array( $users ) ) {
return $count;
}
$userlist = implode( ',', array_map( 'absint', $users ) );
$where = get_posts_by_author_sql( $post_type, true, null, $public_only );
$result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
foreach ( $result as $row ) {
$count[ $row[0] ] = $row[1];
}
foreach ( $users as $id ) {
if ( ! isset( $count[ $id ] ) ) {
$count[ $id ] = 0;
}
}
return $count;
}
function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) { global $wpdb; $count = array(); if ( empty( $users ) || ! is_array( $users ) ) { return $count; } $userlist = implode( ',', array_map( 'absint', $users ) ); $where = get_posts_by_author_sql( $post_type, true, null, $public_only ); $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N ); foreach ( $result as $row ) { $count[ $row[0] ] = $row[1]; } foreach ( $users as $id ) { if ( ! isset( $count[ $id ] ) ) { $count[ $id ] = 0; } } return $count; }
function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
	global $wpdb;

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

	$userlist = implode( ',', array_map( 'absint', $users ) );
	$where    = get_posts_by_author_sql( $post_type, true, null, $public_only );

	$result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
	foreach ( $result as $row ) {
		$count[ $row[0] ] = $row[1];
	}

	foreach ( $users as $id ) {
		if ( ! isset( $count[ $id ] ) ) {
			$count[ $id ] = 0;
		}
	}

	return $count;
}

常見問題

FAQs
檢視更多 >