一、核心方法
1.1 使用 wp_count_posts() 函数
// 获取已发布的文章数量
$post_counts = wp_count_posts('post'); // 文章
echo '已发布文章: ' . $post_counts->publish;
$product_counts = wp_count_posts('product'); // 产品
echo '已发布产品: ' . $product_counts->publish;
// 获取所有状态的数量
$total_posts = $post_counts->publish + $post_counts->draft + $post_counts->pending;
1.2 获取指定状态的数量函数
function get_post_count($post_type = 'post', $status = 'publish') {
$counts = wp_count_posts($post_type);
return isset($counts->$status) ? $counts->$status : 0;
}
// 使用示例
$published_products = get_post_count('product', 'publish');
$draft_posts = get_post_count('post', 'draft');
二、实用快捷函数
2.1 带缓存的获取函数
// 带缓存的获取函数
function get_cached_post_count($post_type, $status = 'publish') {
$cache_key = "post_count_{$post_type}_{$status}";
$count = wp_cache_get($cache_key, 'post_counts');
if ($count === false) {
$counts = wp_count_posts($post_type);
$count = isset($counts->$status) ? $counts->$status : 0;
wp_cache_set($cache_key, $count, 'post_counts', 3600); // 缓存1小时
}
return $count;
}
2.2 批量获取函数
// 批量获取多个文章类型的数量
function get_bulk_post_counts($post_types, $status = 'publish') {
$results = [];
foreach ($post_types as $type) {
$results[$type] = get_post_count($type, $status);
}
return $results;
}
// 使用示例
$counts = get_bulk_post_counts(['post', 'page', 'product'], 'publish');
echo "文章: {$counts['post']}, 页面: {$counts['page']}, 产品: {$counts['product']}";
三、实际应用示例
3.1 在主题中显示文章数量
// 在主题文件中使用
<div class="post-stats">
<h3>网站内容统计</h3>
<ul>
<li>文章: <?php echo get_post_count('post', 'publish'); ?>篇</li>
<li>产品: <?php echo get_post_count('product', 'publish'); ?>个</li>
<li>页面: <?php echo get_post_count('page', 'publish'); ?>页</li>
</ul>
</div>
3.2 后台管理菜单添加统计
// 在functions.php中添加
add_filter('admin_menu', 'add_post_count_to_menu');
function add_post_count_to_menu() {
global $menu;
// 为文章菜单添加数量
$post_count = get_post_count('post', 'publish');
foreach ($menu as $key => $item) {
if ($item[2] == 'edit.php') {
$menu[$key][0] .= " <span class='update-plugins count-{$post_count}'><span class='plugin-count'>{$post_count}</span></span>";
}
}
}
3.3 创建短代码
// 创建短代码 [post_count type="post" status="publish"]
add_shortcode('post_count', 'post_count_shortcode');
function post_count_shortcode($atts) {
$atts = shortcode_atts([
'type' => 'post',
'status' => 'publish',
'label' => 'true'
], $atts);
$count = get_post_count($atts['type'], $atts['status']);
$output = $count;
if ($atts['label'] === 'true') {
$post_type_obj = get_post_type_object($atts['type']);
$type_name = $post_type_obj ? $post_type_obj->labels->name : $atts['type'];
$output = "{$type_name}: {$count}";
}
return $output;
}
// 使用示例
// [post_count type="product"] // 显示产品数量
// [post_count type="post" status="draft"] // 显示草稿数量
四、高级优化技巧
4.1 自定义数据库查询(性能最好)
function get_post_count_fast($post_type, $status = 'publish') {
global $wpdb;
$query = $wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->posts}
WHERE post_type = %s AND post_status = %s",
$post_type,
$status
);
return (int) $wpdb->get_var($query);
}
4.2 清除缓存时机
// 在文章发布/删除时清除缓存
add_action('save_post', 'clear_post_count_cache');
add_action('delete_post', 'clear_post_count_cache');
function clear_post_count_cache($post_id) {
$post_type = get_post_type($post_id);
wp_cache_delete("post_count_{$post_type}_publish", 'post_counts');
wp_cache_delete("post_count_{$post_type}_draft", 'post_counts');
}
五、使用示例汇总
5.1 基本使用
// 获取各种文章类型数量
$posts_count = get_post_count('post'); // 默认获取已发布的
$draft_posts = get_post_count('post', 'draft');
$products_count = get_post_count('product');
$pages_count = get_post_count('page');
5.2 在循环中显示
// 在文章循环中显示分类文章数量
if (have_posts()) {
while (have_posts()) {
the_post();
$categories = get_the_category();
foreach ($categories as $category) {
$count = get_category_post_count($category->term_id);
echo "{$category->name} ({$count}篇文章)";
}
}
}
function get_category_post_count($category_id) {
$args = [
'category__in' => [$category_id],
'post_status' => 'publish',
'posts_per_page' => 1,
'fields' => 'ids',
'no_found_rows' => false,
];
$query = new WP_Query($args);
return $query->found_posts;
}
六、注意事项
- 性能:频繁调用时使用缓存版本
- 权限:确保用户有权限查看数据
- 准确性:考虑文章状态(publish、draft、pending等)
- 多站点:在多站点环境下使用
switch_to_blog()切换
七、一句话解决方案
// 最简单的方法
$count = wp_count_posts('your_post_type')->publish;
// 带缓存的简单方法
$count = get_cached_post_count('your_post_type', 'publish');
选择方法的原则:
大量统计用自定义查询
简单需求用 wp_count_posts()
频繁调用用缓存版本


湘公网安备43020002000238