Skip to content

Instantly share code, notes, and snippets.

@Pleiades
Created November 15, 2012 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pleiades/4082312 to your computer and use it in GitHub Desktop.
Save Pleiades/4082312 to your computer and use it in GitHub Desktop.
Show Blog Statistics
add_shortcode('wcs_count', 'wcs_count_shortcode_handler');
function wcs_count_shortcode_handler($atts)
{
// extract parameters
$parms = shortcode_atts(array(
'type' => 'posts',
'format' => 'true',
'extra' => '1',
), $atts);
$type = strtolower($parms['type']);
$format = strtolower($parms['format']);
$extra = $parms['extra'];
// process t/f options
$b_format = false;
if (($format == 'yes') || ($format == 'y') ||
($format == 'true') || ($format == '1'))
{$b_format = true;}
// exit
return wcs_get_count($type, $b_format, $extra);
}
function wcs_get_count($type='posts', $format='1', $extra='1')
{
// TYPES:
// posts, posts_by_author, pages, tags, categories
// users, ms_users, blogroll, blogroll_categories, commenters
// comments, comments_pending, comments_spam, comments_pingback
// comments_by_user, comments_by_nicename, comments_by_email
// comments_per_post
// $extra is used with:
// posts_by_author, comments_by_user, comments_by_nicename, comments_by_email
// comments_per_post
// init
global $wpdb;
$type = strtolower($type);
$count = 0;
// process
switch($type)
{
case 'posts': // published
$count = wp_count_posts('post');
$count = $count->publish;
// options: publish, future, draft, pending, private, trash, auto-draft, & inherit
case 'posts_by_author': // use $extra for user/author id
case 'posts_by_user':
$query = "SELECT COUNT(*) FROM $wpdb->posts ";
$where = "WHERE post_type='post' AND post_status='publish' AND post_author='$extra'";
$count = $wpdb->get_var($query . $where);
// alternative method is: count_user_posts()
break;
case 'pages': // published
$count = wp_count_posts('page');
$count = $count->publish;
break;
case 'tags':
$count = wp_count_terms('post_tag');
break;
case 'categories':
$count = wp_count_terms('category');
break;
case 'users':
$count = count_users();
$count = $count['total_users'];
break;
case 'ms_users': // multi-site
$count = get_user_count();
break;
case 'blogroll':
$query = "SELECT COUNT(*) FROM $wpdb->links ";
$where = "WHERE link_visible='Y'";
$count = $wpdb->get_var($query . $where);
break;
case 'blogroll_categories':
$count = wp_count_terms('link_category');
break;
case 'commenters':
$query = "SELECT COUNT(DISTINCT comment_author) FROM $wpdb->comments ";
$where = "WHERE comment_approved='1' AND comment_type=''";
$count = $wpdb->get_var($query . $where);
break;
case 'comments':
$query = "SELECT COUNT(*) FROM $wpdb->comments ";
$where = "WHERE comment_approved='1' AND comment_type=''";
$count = $wpdb->get_var($query . $where);
break;
case 'comments_pending':
$query = "SELECT COUNT(*) FROM $wpdb->comments ";
$where = "WHERE comment_approved='0' AND comment_type=''";
$count = $wpdb->get_var($query . $where);
break;
case 'comments_spam':
$query = "SELECT COUNT(*) FROM $wpdb->comments ";
$where = "WHERE comment_approved='spam' AND comment_type=''";
$count = $wpdb->get_var($query . $where);
break;
case 'comments_pingback':
case 'comments_pingbacks':
case 'comments_trackback':
case 'comments_trackbacks':
$query = "SELECT COUNT(*) FROM $wpdb->comments ";
$where = "WHERE comment_approved='1' AND comment_type='pingback'";
$count = $wpdb->get_var($query . $where);
break;
case 'comments_by_user': // use $extra for user_id
$query = "SELECT COUNT(*) FROM $wpdb->comments ";
$where = "WHERE comment_approved='1' AND comment_type='' AND user_id='$extra'";
$count = $wpdb->get_var($query . $where);
break;
case 'comments_by_author': // use $extra for author nicename (case INsensitive)
case 'comments_by_nicename':
$query = "SELECT COUNT(*) FROM $wpdb->comments ";
$where = "WHERE comment_approved='1' AND comment_type='' AND comment_author='$extra'";
$count = $wpdb->get_var($query . $where);
break;
case 'comments_by_email': // use $extra for author email (case INsensitive)
$query = "SELECT COUNT(*) FROM $wpdb->comments ";
$where = "WHERE comment_approved='1' AND comment_type='' AND comment_author_email='$extra'";
$count = $wpdb->get_var($query . $where);
break;
case 'comments_per_post': // $extra is decimal place precision (0 for integer only)
// posts
$posts_count = wp_count_posts('post');
$posts_count = $posts_count->publish;
// comments
$query = "SELECT COUNT(*) FROM $wpdb->comments ";
$where = "WHERE comment_approved='1' AND comment_type=''";
$comment_count = $wpdb->get_var($query . $where);
// average
return round($comment_count / $posts_count, $extra);
default:
$count = 0;
}
// exit
if ($format) {$count = number_format_i18n($count);}
return $count;
/
Copyright © 2011 Gizmo Digital Fusion (http://wpCodeSnippets.info)
you can redistribute and/or modify this code under the terms of the
GNU GPL v2: http://www.gnu.org/licenses/gpl-2.0.html
}
[wcs_count type="posts"]  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment