wp_count_posts

函数
wp_count_posts ( $type = 'post', $perm = '' )
参数
  • (string) $type Optional. Post type to retrieve count. Default 'post'.
    Required:
    Default: 'post'
  • (string) $perm Optional. 'readable' or empty. Default empty.
    Required:
    Default: (empty)
返回值
  • (stdClass) Number of posts for each status.
定义位置
相关方法
wp_count_siteswp_count_termswp_count_commentswp_untrash_postcount_user_posts
引入
2.5.0
弃用
-

wp_count_posts: 这是一个返回特定文章类型的文章数量的函数。它可以用来获得文章类型的概述,并跟踪文章的数量。

计算某个文章类型的文章数量,以及用户是否有查看的权限。

这个函数提供了一个有效的方法来查找一个博客的文章类型的数量。另一种方法是在get_posts()中计算项目的数量,但这种方法有很多开销。因此,在为2.5以上版本开发时,请使用这个函数来代替。

$perm参数检查’可读’值,如果用户可以阅读私人文章,它将为已登录的用户显示。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_count_posts( $type = 'post', $perm = '' ) {
global $wpdb;
if ( ! post_type_exists( $type ) ) {
return new stdClass;
}
$cache_key = _count_posts_cache_key( $type, $perm );
$counts = wp_cache_get( $cache_key, 'counts' );
if ( false !== $counts ) {
// We may have cached this before every status was registered.
foreach ( get_post_stati() as $status ) {
if ( ! isset( $counts->{$status} ) ) {
$counts->{$status} = 0;
}
}
/** This filter is documented in wp-includes/post.php */
return apply_filters( 'wp_count_posts', $counts, $type, $perm );
}
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
if ( 'readable' === $perm && is_user_logged_in() ) {
$post_type_object = get_post_type_object( $type );
if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
$query .= $wpdb->prepare(
" AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
get_current_user_id()
);
}
}
$query .= ' GROUP BY post_status';
$results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
$counts = array_fill_keys( get_post_stati(), 0 );
foreach ( $results as $row ) {
$counts[ $row['post_status'] ] = $row['num_posts'];
}
$counts = (object) $counts;
wp_cache_set( $cache_key, $counts, 'counts' );
/**
* Modifies returned post counts by status for the current post type.
*
* @since 3.7.0
*
* @param stdClass $counts An object containing the current post_type's post
* counts by status.
* @param string $type Post type.
* @param string $perm The permission to determine if the posts are 'readable'
* by the current user.
*/
return apply_filters( 'wp_count_posts', $counts, $type, $perm );
}
function wp_count_posts( $type = 'post', $perm = '' ) { global $wpdb; if ( ! post_type_exists( $type ) ) { return new stdClass; } $cache_key = _count_posts_cache_key( $type, $perm ); $counts = wp_cache_get( $cache_key, 'counts' ); if ( false !== $counts ) { // We may have cached this before every status was registered. foreach ( get_post_stati() as $status ) { if ( ! isset( $counts->{$status} ) ) { $counts->{$status} = 0; } } /** This filter is documented in wp-includes/post.php */ return apply_filters( 'wp_count_posts', $counts, $type, $perm ); } $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; if ( 'readable' === $perm && is_user_logged_in() ) { $post_type_object = get_post_type_object( $type ); if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) { $query .= $wpdb->prepare( " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))", get_current_user_id() ); } } $query .= ' GROUP BY post_status'; $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); $counts = array_fill_keys( get_post_stati(), 0 ); foreach ( $results as $row ) { $counts[ $row['post_status'] ] = $row['num_posts']; } $counts = (object) $counts; wp_cache_set( $cache_key, $counts, 'counts' ); /** * Modifies returned post counts by status for the current post type. * * @since 3.7.0 * * @param stdClass $counts An object containing the current post_type's post * counts by status. * @param string $type Post type. * @param string $perm The permission to determine if the posts are 'readable' * by the current user. */ return apply_filters( 'wp_count_posts', $counts, $type, $perm ); }
function wp_count_posts( $type = 'post', $perm = '' ) {
	global $wpdb;

	if ( ! post_type_exists( $type ) ) {
		return new stdClass;
	}

	$cache_key = _count_posts_cache_key( $type, $perm );

	$counts = wp_cache_get( $cache_key, 'counts' );
	if ( false !== $counts ) {
		// We may have cached this before every status was registered.
		foreach ( get_post_stati() as $status ) {
			if ( ! isset( $counts->{$status} ) ) {
				$counts->{$status} = 0;
			}
		}

		/** This filter is documented in wp-includes/post.php */
		return apply_filters( 'wp_count_posts', $counts, $type, $perm );
	}

	$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";

	if ( 'readable' === $perm && is_user_logged_in() ) {
		$post_type_object = get_post_type_object( $type );
		if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
			$query .= $wpdb->prepare(
				" AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
				get_current_user_id()
			);
		}
	}

	$query .= ' GROUP BY post_status';

	$results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
	$counts  = array_fill_keys( get_post_stati(), 0 );

	foreach ( $results as $row ) {
		$counts[ $row['post_status'] ] = $row['num_posts'];
	}

	$counts = (object) $counts;
	wp_cache_set( $cache_key, $counts, 'counts' );

	/**
	 * Modifies returned post counts by status for the current post type.
	 *
	 * @since 3.7.0
	 *
	 * @param stdClass $counts An object containing the current post_type's post
	 *                         counts by status.
	 * @param string   $type   Post type.
	 * @param string   $perm   The permission to determine if the posts are 'readable'
	 *                         by the current user.
	 */
	return apply_filters( 'wp_count_posts', $counts, $type, $perm );
}

常见问题

FAQs
查看更多 >