# count_user_posts()

URL: https://chugunov.pro/api-wordpress/functions/count_user_posts/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 3.0.0.

## Сигнатура

```php
count_user_posts( int $userid, array|string $post_type = 'post', bool $public_only = false ): string
```

## Описание

Возвращает количество записей, написанных пользователем.

## Параметры

- `$userid` `int` — обязательный. ID пользователя.
- `$post_type` `array|string` — необязательный, по умолчанию `'post'`. Один тип записи или массив типов записей, для которых подсчитывается количество записей. Значение по умолчанию 'post'.
- `$public_only` `bool` — необязательный, по умолчанию `false`. Возвращать ли количество только для публичных записей.

## Возвращаемое значение

`string`

## Исходный код

Файл: `wp-includes/user.php:616`

```php
function count_user_posts( $userid, $post_type = 'post', $public_only = false ) {
	global $wpdb;

	$post_type = array_unique( (array) $post_type );
	sort( $post_type );

	$where = get_posts_by_author_sql( $post_type, true, $userid, $public_only );
	$query = "SELECT COUNT(*) FROM $wpdb->posts $where";

	$last_changed = wp_cache_get_last_changed( 'posts' );
	$cache_key    = 'count_user_posts:' . md5( $query );
	$count        = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed );
	if ( false === $count ) {
		$count = $wpdb->get_var( $query );
		wp_cache_set_salted( $cache_key, $count, 'post-queries', $last_changed );
	}

	/**
	 * Filters the number of posts a user has written.
	 *
	 * @since 2.7.0
	 * @since 4.1.0 Added `$post_type` argument.
	 * @since 4.3.1 Added `$public_only` argument.
	 *
	 * @param string       $count       The user's post count as a numeric string.
	 * @param int          $userid      User ID.
	 * @param string|array $post_type   Single post type or array of post types to count the number of posts for.
	 * @param bool         $public_only Whether to limit counted posts to public posts.
	 */
	return apply_filters( 'get_usernumposts', $count, $userid, $post_type, $public_only );
}
```

## История изменений

- 4.3.0 — Added $public_only argument. Added the ability to pass an array of post types to $post_type.
- 4.1.0 — Added $post_type argument.
- 3.0.0 — Introduced.

## Связанные

Использует: [`wp_cache_get_salted`](https://chugunov.pro/api-wordpress/functions/wp_cache_get_salted/), [`wp_cache_set_salted`](https://chugunov.pro/api-wordpress/functions/wp_cache_set_salted/), [`wp_cache_get_last_changed`](https://chugunov.pro/api-wordpress/functions/wp_cache_get_last_changed/), [`get_posts_by_author_sql`](https://chugunov.pro/api-wordpress/functions/get_posts_by_author_sql/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), `wpdb::get_var`.
Используется в: `WP_REST_Users_Controller::get_item_permissions_check`, [`get_usernumposts`](https://chugunov.pro/api-wordpress/functions/get_usernumposts/), [`get_the_author_posts`](https://chugunov.pro/api-wordpress/functions/get_the_author_posts/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/count_user_posts/
