<?php
/*
Plugin Name: Category Word Count
Plugin URI: http://guff.szub.net/category-word-count
Description: Outputs the total number of words in a category.
Version: R1.1
Author: Kafkaesquí Oseo
Author URI: http://szub.net
Props to Nick Momrik ( http://mtdewvirus.com/ ), upon whose
'Post Word Count' plugin my own word count functionality is
stole...er, based.
~Change log:
R1.1 (30-Oct-2004)
Added functionality for 'all' categories, if this is passed
to the script.
*/
function cat_word_count($catid='', $preface='') {
global $wpdb, $cat, $post, $tableposts, $tablepost2cat;
global $postcats, $words, $totalcount;
$now = gmdate("Y-m-d H:i:s",time());
if (empty($catid)) {
$catid = $cat;
}
if ($catid) {
if ($catid == 'all') {
$words = $wpdb->get_results("SELECT post_content FROM $tableposts WHERE post_status = 'publish' AND post_date < '$now'");
foreach ($words as $word) {
$post = strip_tags($word->post_content);
$post = explode(' ', $post);
$count = count($post);
$totalcount = $count + $oldcount;
$oldcount = $totalcount;
}
if (empty($preface)) {
$preface = 'Word count for all categories: ';
}
echo $preface.number_format($totalcount);
} else {
$postcats = $wpdb->get_results("SELECT post_id FROM $tablepost2cat WHERE category_id = '$catid'");
$words = $wpdb->get_results("SELECT ID, post_content FROM $tableposts WHERE post_status = 'publish' AND post_date < '$now'");
if ($postcats) {
foreach ($postcats as $postcat) {
$postcat_id = $postcat->post_id;
foreach ($words as $word) {
$word_id = $word->ID;
if ($word_id == $postcat_id) {
$post = strip_tags($word->post_content);
$post = explode(' ', $post);
$count = count($post);
$totalcount = $count + $oldcount;
$oldcount = $totalcount;
}
}
}
}
if (empty($preface)) {
$preface = get_the_category_by_ID($catid).' word count: ';
}
echo $preface.number_format($totalcount);
}
}
}
?>