Count words for all posts by all authors

You will need a function to get all published posts and count the words.

function wpse410818_count_published_words() {

    $posts = new WP_Query(array(
        'post_type' => array( 'post' ),
        'post_status' => 'publish',
    ));
    
    $count = 0; //Starting words count
    if ( $posts->have_posts() ) {
        while ( $posts->have_posts() ) {
            $posts->the_post();
            //Add to the word count
            $count += str_word_count(trim(strip_tags(strip_shortcodes(get_the_content()))));
        }
    }
    
    echo $count;
}

It will query the database every time it is run, therefore, you want to cache the result and update it when a new post is published or an existing post is modified.

Leave a Comment