Blog / WordPress/ WordPress Site Statistics Code Collection (Updated)

WordPress Site Statistics Code Collection (Updated)

WordPress 站点数据统计代码大全( 更新版)

When developing WordPress themes, we often need to retrieve various site statistics, such as the total number of published posts, comments, tags, categories, and pages. This data is useful for displaying site overviews or building statistical widgets.

This article compiles a series of commonly used WordPress statistics code snippets for developers to use directly.

Common Site Statistics Code

1. Total Published Posts

<?php
$count_posts = wp_count_posts();
echo $count_posts->publish;
?>

Explanation: wp_count_posts() returns an object; the ->publish property is the count of published posts.

2. Total Comments

<?php
echo get_comments_number(); // Recommended method
?>

Explanation: Use the built-in get_comments_number() for safety and caching. For all comments (including pending), use a database query.

3. Total Tags

<?php
$tags = wp_count_terms('post_tag');
echo (is_wp_error($tags)) ? 0 : $tags;
?>

Explanation: Use wp_count_terms() and check for WP_Error to improve robustness.

4. Total Categories

<?php
$categories = wp_count_terms('category');
echo (is_wp_error($categories)) ? 0 : $categories;
?>

5. Total Published Pages

<?php
$count_pages = wp_count_posts('page');
echo $count_pages->publish;
?>

6. Total Users

<?php
$user_count = count_users();
echo $user_count['total_users'];
?>

Explanation: count_users() returns an array; total_users is the user count.

7. Site Uptime in Days

<?php
// Replace '2020-01-01' with your actual start date
$start_date = strtotime('2020-01-01');
$days = floor((time() - $start_date) / 86400);
echo $days;
?>

Explanation: Calculates days elapsed from a specified start date to now.

8. Site Last Updated Time

<?php
$last_modified = get_lastpostmodified('blog');
echo $last_modified ? date('Y-m-d', strtotime($last_modified)) : 'No updates yet';
?>

Explanation: Use get_lastpostmodified() for reliable last modified time of posts/pages.

9. Total Blogroll Links (If Link Manager Enabled)

<?php
// Requires Link Manager feature (disabled by default)
global $wpdb;
$link_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'");
echo $link_count;
?>

Explanation: This depends on the 'Link Manager' being enabled (managed under Dashboard -> Links).

Advanced Statistics Examples

Get Post Count for a Specific Category

<?php
$category_id = 5; // Replace 5 with target category ID
$category = get_category($category_id);
if ($category) {
    echo $category->count;
}
?>

Get Post Count for a Custom Post Type (CPT)

<?php
$post_type = 'product'; // Replace 'product' with your CPT name
$count = wp_count_posts($post_type);
echo $count->publish;
?>

Usage Tips

1. When using these snippets directly in theme files, ensure they are within PHP tags <?php ... ?>.
2. For frequently called statistics, consider caching with WordPress Transients API to improve performance.
3. Some statistics (like user count) are also visible in the wp-admin Dashboard.

With the above code, you can easily integrate various statistics into your WordPress theme or plugin.

Post a Comment

Your email will not be published. Required fields are marked with *.