Web & Software Developer

Count Words in WordPress Post Content

Here are three examples of how to count words in WordPress post content. You can use this to count the words in one post (or page, or any custom post type). Or, you can use this to display the word count on the current page (or post, or any custom post type). Or, you can loop through all posts and count the words in order to find all posts with a certain number of words.

Mandatory step: This is the function that does the actual counting. All three examples use this function, so you’ll need to add this to your functions:

/**
 * Count the number of words in post content
 * @param string $content The post content
 * @return integer $count Number of words in the content
 */
function isa_count_content_words( $content ) {
    $decode_content = html_entity_decode( $content );
    $filter_shortcode = do_shortcode( $decode_content );
    $strip_tags = wp_strip_all_tags( $filter_shortcode, true );
    $count = str_word_count( $strip_tags );
    return $count;
}

Usage Example 1

To count the words in a single post, page, or custom post type, use this. Replace 123 on line 1, with the ID of the post that you want to count words for.

$post_id = 123;
$post_object = get_post( $post_id );
$content = $post_object->post_content;
$word_count = isa_count_content_words( $content );

Usage Example 2

If you want to display the word count of the current post, you can place this snippet directly in the loop, or in a template file such as single.php:

<?php
$content = apply_filters( 'the_content', get_the_content() );
$word_count = isa_count_content_words( $content );
echo $word_count;
?>

The last code snippet can go directly into a template file, but there is a cleaner way. You can make a template tag. To make a template tag, place this in your functions:

/**
 * Template tag to display word count.
 * @param string $before What to insert before the number.
 * @param string $after What to insert after the number.
 */
function isa_wordcount( $before = '', $after = ' words.' ) {
	global $post;
	if ( ! empty( $post ) ) {
		$word_count = isa_count_content_words( $post->post_content );
		echo $before . $word_count . $after;
	}
}

Then, you can use the template tag in any template file where you want to display the word count of the current single post or page. This will also work for custom post types.

For example, place this in a template file to display the word count with PHP:

<?php
isa_wordcount();
?>

By default, that template tag will display the number, followed by words., like this:

365 words.

You can change what appears before and after the number by passing parameters. The first parameter is what to add before the number, and the second parameter is what to add after the number.

So, to display this:

There are 365 words in this article.

…use this:

isa_wordcount( 'There are ', ' words in this article.' );

Or, to show only the number, pass two empty parameters, like this:

isa_wordcount( '', '' );

Usage Example 3

This example is useful if you need to search posts by word count. For example, you may want to search through all posts to find which posts have more than 1000 words. Or which posts have less than 300 words.

This snippet finds all posts that have more than 1000 words. To also include posts that have exactly 1000 words, change the “greater than” symbol > on line 21 to >= which will mean “greater than or equal to.”

The variable $long_posts will hold the list of results in an array. Each element of the array will have the post ID as the key, and the post title as the value.

$target_wordcount = 1000;

$long_posts = array();

$args = array(
  'post_type'   => 'post',
  'post_status' => 'publish',
  'numberposts' => -1
);

$all_posts = get_posts( $args );

if ( $all_posts ) {

	foreach ( $all_posts as $p ) {

		$this_count = isa_count_content_words( $p->post_content );

		// Check if this post has MORE words than our target count
		
		if ( $this_count > $target_wordcount ) {

			// Add this post ID and title to our array

			$long_posts[ $p->ID ] = $p->post_title;


	    }
			    
	}
}

If instead, you want to find all posts that have less than 300 words, use this:

$target_wordcount = 300;

$short_posts = array();

$args = array(
  'post_type'   => 'post',
  'post_status' => 'publish',
  'numberposts' => -1
);

$all_posts = get_posts( $args );

if ( $all_posts ) {

	foreach ( $all_posts as $p ) {

		$this_count = isa_count_content_words( $p->post_content );

		// Check if this post has LESS words than our target count

		if ( $this_count < $target_wordcount ) {

			// Add this post ID and title to our array

			$short_posts[ $p->ID ] = $p->post_title;


	    }

			    
	}

}

The variable $short_posts will hold the list of results in an array. Each element of the array will have the post ID as the key, and the post title as the value.

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]