функция
since 3.0.0
count_user_posts()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
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
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. |

