返回介绍

wp_count_posts()

发布于 2017-09-11 11:43:30 字数 6846 浏览 1098 评论 0 收藏 0

wp_count_posts( string $type = 'post',  string $perm = '' )

Count number of posts of a post type and if user has permissions to view.


description

This function provides an efficient method of finding the amount of post’s type a blog has. Another method is to count the amount of items in get_posts(), but that method has a lot of overhead with doing so. Therefore, when developing for 2.5+, use this function instead.

The $perm parameter checks for ‘readable’ value and if the user can read private posts, it will display that for the user that is signed in.


参数

$type

(string) (Optional) Post type to retrieve count.

Default value: 'post'

$perm

(string) (Optional) 'readable' or empty.

Default value: ''


返回值

(object) Number of posts for each status.


源代码

File: wp-includes/post.php

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 ) {
		/** 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' );

	/**
	 * Modify returned post counts by status for the current post type.
	 *
	 * @since 3.7.0
	 *
	 * @param object $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 );
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/capabilities.php: current_user_can()
  • wp-includes/cache.php: wp_cache_get()
  • wp-includes/cache.php: wp_cache_set()
  • wp-includes/pluggable.php: is_user_logged_in()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/user.php: get_current_user_id()
  • wp-includes/post.php: _count_posts_cache_key()
  • wp-includes/post.php: wp_count_posts
  • wp-includes/post.php: post_type_exists()
  • wp-includes/post.php: get_post_type_object()
  • wp-includes/post.php: get_post_stati()
  • wp-includes/wp-db.php: wpdb::get_results()
  • wp-includes/wp-db.php: wpdb::prepare()
  • Show 8 more uses Hide more uses

Used By

  • wp-admin/includes/dashboard.php: wp_dashboard_right_now()
  • wp-admin/includes/post.php: get_available_post_statuses()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::get_views()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::prepare_items()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Basic Example

    The default usage returns a count of the posts that are published. This will be an object, you can var_dump() the contents to debug the output.

    
    $count_posts = wp_count_posts();
    
  2. Get the Publish Status Post Count

    To get the published status type, you would call the wp_count_posts() function and then access the ‘publish’ property.

    
    $count_posts = wp_count_posts();
    
    if ( $count_oosts ) {
    	$published_posts = $count_posts->publish;
    }
    

    If you are developing for PHP5 only, then you can use shorthand, if you only want to get one status. This will not work in PHP4 and if you want to maintain backwards compatibility, then you must use the above code.

    
    $published_posts = wp_count_posts()->publish;
    

    Count Drafts

    Counting drafts is handled the same way as the publish status.

    
    $count_posts = wp_count_posts();
    
    if ( $count_posts ) {
    	$draft_posts = $count_posts->draft;
    }
    

    Count Pages

    Counting pages status types are done in the same way as posts and make use of the first parameter. Finding the number of posts for the post status is done the same way as for posts.

    
    $count_pages = wp_count_posts( $post_type = 'page' );
    

    Other Uses

    The wp_count_posts() can be used to find the number for post statuses of any post type. This includes attachments or any post type added in the future, either by a plugin or part of the WordPress Core.

    
    $count_posts = wp_count_posts();
     
    if ( $count_oosts ) {
        $published_posts = $count_posts->publish;
    }
    

    has a typo, it should be

    
    $count_posts = wp_count_posts();
     
    if ( $count_posts ) {
        $published_posts = $count_posts->publish;
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文