How can I count words in posts + tags + comments and display the number of total words used by each author ?

1 Answer 1


Run a couple of queries and a code snippet to calculate the word count for the post content, comments content and tag names on each post.

// This query will return the number of words in the post content
$post_qry="SELECT LENGTH(post_content) - LENGTH(REPLACE(post_content, ' ', ''))+1 
from wp_posts where ID=".$post_id;
// This query will return the number of words in all comments for this post
$comment_qry="SELECT SUM(LENGTH(comment_content) - LENGTH(REPLACE(comment_content, ' ', ''))+1) 
from wp_comments where comment_post_ID=".$post_id;

$post_tags = get_the_tags();
if ($post_tags) {
  foreach($post_tags as $tag)
    $parts=explode(' ',$tag->name);

I didn't include any code to execute the queries because everyone seems to have their own ways of doing that. The biggest flaw in this approach is that it counts all space-separated strings. If your authors can do media inserts, then those captioned images/videos will inflate the word count.

If near perfection is required, then change the queries to just get all the text (don't count in mysql). Then run some regex functions to strip out HTML and WP special content like media items. Finally, explode the remaining strings on space and count the arrays.

I don't know how to reach absolute perfection. For me, the first approach is good enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.