WordPress获取指定文章类型的文章数量

本文介绍了WordPress中的wp_count_posts()函数,用于统计指定文章类型的文章数量。该函数接受两个参数:文章类型和权限检查,返回包含不同状态文章数量的对象。文章详细说明了如何获取默认文章类型和自定义文章类型的文章数,并提供了代码示例和返回数据结构解析。

WordPress基础教程
阅读时间: 31 分钟
最后更新时间:2025年11月13日

需要调用数指定文章类型的文章数量,找到了 wp_count_posts() 函数,很方便就实现了。

wp_count_posts() 介绍

wp_count_posts() 函数是用来计算文章类型的文章数量的,还可以设置用户是否有权查看。有两个可用参数:

wp_count_posts( string $type = 'post', string $perm = '' )
  • $type :字符串,可选 。获取指定的文章类型的文章数,默认为: 'post',可以填写文章类型的名称。
  • $perm : 字符串,可选 。检查“可读”值,如果用户可以阅读私密文章,它将为登录用户显示该文章。 可选值 readable 或 空,默认为空 ''

获取默认文章的文章数

如果我们要调用默认的 文章 的文章数量,可以使用下面的代码:

$count_posts = wp_count_posts(); //里面没有参数,默认为 post
 
if ( $count_posts ) {
    $published_posts = $count_posts->publish;
}

wp_count_posts() 函数返回的是一个数组,里面可能包含数据为:

stdClass Object ( [publish] => 12 [future] => 0 [draft] => 0 [pending] => 0 [private] => 0 [trash] => 1 [auto-draft] => 1 [inherit] => 0 [request-pending] => 0 [request-confirmed] => 0 [request-failed] => 0 [request-completed] => 0 ) 1

从中可以看到,可以针对文章的发布状态进行显示。比如在上面的示例代码中,我们采用 $count_posts->publish; 返回的是已发布的文章数量。

获取自定义文章类型的文章数

上面我们已经介绍了, wp_count_posts() 函数的第一个参数是文章类型名称,可以是 postpage 等这种内置的文章类型,也可以是自定义的文章类型。

那么,如果知道文章类型的名称呢,有一个比较直观的办法就是,在后台访问这个文章类型的管理界面:

然后我们就可以在网址栏中看到 post_type=page ,那么 page 就是这个文章类型名称了。

专业一点的话,我们需要去找到这个自定义文章类型的注册代码,在这里需要大家去脑补一下自定义文章类型的注册方式:

下面是一个官方的示例:

add_action( 'init', 'codex_book_init' );
/**
 * Register a book post type.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_post_type
 */
function codex_book_init() {
	$labels = array(
		'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
		'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
		'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
		'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
		'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
		'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
		'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
		'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
		'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
		'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
		'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
		'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
		'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
		'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
	);

	$args = array(
		'labels'             => $labels,
		'description'        => __( 'Description.', 'your-plugin-textdomain' ),
		'public'             => true,
		'publicly_queryable' => true,
		'show_ui'            => true,
		'show_in_menu'       => true,
		'query_var'          => true,
		'rewrite'            => array( 'slug' => 'book' ),
		'capability_type'    => 'post',
		'has_archive'        => true,
		'hierarchical'       => false,
		'menu_position'      => null,
		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
	);

	register_post_type( 'book', $args );
}

注意看倒数的 register_post_type( 'book', $args ); 里面的 book 就是文章类型的名称。所以,如果我们要调用 book 这个文章类型的文章数量,可以使用下面的代码:

$count_posts = wp_count_posts('book'); //参数填写为 book
 
if ( $count_posts ) {
    $published_posts = $count_posts->publish;
}

好了,就是这样!

这篇文章有用吗?

点击星号为它评分!

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

在AI里面继续讨论:

曾凤祥

曾凤祥

WordPress技术负责人
小兽WordPress凭借15年的WordPress企业网站开发经验,坚持以“为企业而生的WordPress服务”为宗旨,累计为10万多家客户提供高品质WordPress建站服务,得到了客户的一致好评。我们一直用心对待每一个客户,我们坚信:“善待客户,将会成为终身客户”。小兽WordPress能坚持多年,是因为我们一直诚信。

相关文章

如何让线上业务更上一层楼

还没有WordPress网站

还没有WordPress网站

不管你从事什么行业,WordPress都会为你提供一个专业的主题模板。在WordPress市场上有成千上万的免费主题,适合很多中小企业。

查看所有模板
已经有WordPress网站

已经有WordPress网站

小兽WordPress诚邀你一起学习WordPress,愿与各方携手升级改善您的WordPress网站,一起交流网站加速,网站优化等问题。

马上交个朋友
微信联系
chat 扫码联系
模板建站
挑选模板
网站定制
免费诊断
咨询热线
咨询热线

189-0733-7671

返回顶部