If you have a blog then most likely you would like to know the stats for your blog, such as number of posts, pages, comments, categories, tags etc. All this data is recorded on the Wordpress database you just need to know a few queries to access this data. Displaying this sort of data will be important to any potential advertisers for your blog. If someone is going to pay for ad space then they will be interested in a few small details about the current state of your blog. Below are 10 snippets which you can add to your functions.php page to get different stats for your blog.
Display the amount of user's your blog has.
function user_count() {
global $wpdb;
return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->users);
}
This will get the amount of posts you current have published.
function pu_post_count() {
global $wpdb;
return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->posts . ' WHERE post_status = "publish" AND post_type = "post"');
}
Display the amount of pages your blog currently has.
function pu_page_count() {
global $wpdb;
return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->posts . ' WHERE post_status = "publish" AND post_type = "page"');
}
Count the amount of comments your blog currently has.
function pu_comment_count() {
global $wpdb;
return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->comments . ' WHERE comment_approved = "1"');
}
Count the amount of trackbacks your blog currently has.
function pu_trackback_count() {
global $wpdb;
return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->comments . ' WHERE comment_type = "pingback"');
}
Get the average number of comments each post should get.
function pu_avg_comments_per_post() {
$comment_count = $this->comment_count();
$post_count = $this->post_count();
if ($post_count) {
return round($comment_count/$post_count);
} else {
return 0;
}
}
Get the amount of categories your blog has.
function pu_category_count() {
return count(get_all_category_ids());
}
Get the total number of tags your blog has.
function pu_tag_count() {
global $wpdb;
return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->terms . ' INNER JOIN ' . $wpdb->term_taxonomy . ' ON ' . $wpdb->terms . '.term_id = ' . $wpdb->term_taxonomy . '.term_id WHERE ' . $wpdb->term_taxonomy . '.taxonomy = "post_tag"');
}
Get the total number of links to your blog.
function pu_link_count() {
global $wpdb;
return (int) $wpdb->get_var('SELECT COUNT(*) FROM ' . $wpdb->links . ' WHERE link_visible = "Y"');
}
If you add the above snippets to your functions.php page then you can use any of them to create a shortcode to use in your blog posts. In this example we will use the user_count() function to create a shortcode by adding the following in your functions.php page.
add_shortcode('number_of_users', 'pu_user_count');
Get weekly updates to your email