Show Simple Statistics In WordPress Blog

Show Simple Statistics In WordPress Blog
Show Simple Statistics In WordPress Blog

There are many statistics plugins for WordPress, but if you want to show just some basic simple statistics like total posts, comments, categories, users, etc. you might don’t want to use these plugins. This article will help you to show this information with some simple codes.

Paste the following code into functions.php file of your theme:

function simple_stats() {
    global $wpdb;

    $stats = array();

    $stats['posts'] = number_format_i18n(wp_count_posts('post')->publish);
    $stats['pages'] = number_format_i18n(wp_count_posts('page')->publish);
    $stats['cats']  = number_format_i18n(wp_count_terms('category'));
    $stats['tags'] = number_format_i18n(wp_count_terms('post_tag'));
    $stats['comments'] = number_format_i18n(wp_count_comments()->approved);

    $stats['users'] = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->prefix}users");

    echo '<div class="simple-stats">',
        '<p>Total posts: <b>', $stats['posts'], '</b></p>',
        '<p>Total pages: <b>', $stats['pages'], '</b></p>',
        '<p>Total categories: <b>', $stats['cats'], '</b></p>',
        '<p>Total tags: <b>', $stats['tags'], '</b></p>',
        '<p>Total comments: <b>', $stats['comments'], '</b></p>',
        '<p>Total users: <b>', $stats['users'], '</b></p>',
        '</div>';
}

Once done, you can use the function in theme files to show statistics:

<?php simple_stats(); ?>

Newsletter

Subscribe to our newsletter to receive news and tutorials about WordPress and web development.

Meta Box - Creating expert WordPress custom fields and custom meta boxes in minutes! Check it out →