Word count for all posts of all authors

Question

I currently have 11 posts and the total word count shown in the Gutenberg editor after clicking the “i” icon above the writing area is 2,342 words. I manually added it up. The words in the title are 151 words for all 11 posts.
So the grand total is 2493 words.

However, this code below shows that there are 3,868 words.

What is wrong here?

function getWordCountFromPosts () {
    $count = 0;
    $posts = get_posts( array(
            'numberposts' => -1,
            'post_type' => 'any'
    ));
    foreach( $posts as $post ) {
        $count += str_word_count( strip_tags( get_post_field( 'post_content', $post->ID )));
    }
    $num =  number_format_i18n( $count );
    return $num;
}

I displayed the result using

<?php echo getWordCountFromPosts(); ?>

0
Elaine Byene 1 year 2022-11-02T00:08:30-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse