How can I get a total word count of one author's posts? Thanks.
To be more clear, I wanna show the total word count in the author archive page, preferably using code.
How can I get a total word count of one author's posts? Thanks.
To be more clear, I wanna show the total word count in the author archive page, preferably using code.
Here's a basic concept how I'd do the word counting and showing the count. Hopefully this serves as a starting point.
I think it would be a good idea to store the word count in a transient, so it isn't calculated on each and every author archive page load.
function author_word_count() {
// get current author
$author_id = get_queried_object_id();
// check if there's valid word count transient, show that if so
$word_count = get_transient( $author_id . '_word_count' );
if ( $word_count ) {
echo $word_count;
} else {
// fallback to calculating word count, show it and save it as transient
$word_count = calculate_author_posts_words( $author_id );
echo $word_count;
}
}
Helper function for calculation
function calculate_author_posts_words( $author_id ) {
$author_posts = get_author_posts( $author_id );
$count = 0;
if ( ! empty( $author_posts->posts ) ) {
// If you have gazillion posts, then this might hit your server hard I guess.
// There might be more performant ways to doing this, but I can't think of any right now
foreach( $author_posts->posts as $p ) {
$count = $count + prefix_wcount( $p->post_content );
}
}
set_transient( $author_id . '_word_count', $count, $expiration ); // Save the count, set suitable expiration time
return $count;
}
Helper function to get authors posts
function get_author_posts( $id ) {
// Do WP_Query with author's id and return it
}
Helper function to count single post's word count. Modified from Counting words in a post
function prefix_wcount( $post_content ){
return sizeof(explode(" ", $post_content));
}
You could also hook a custom update function to save_post
to update the word count transient when a new post is made or and existing one is updated.
function update_word_count_transient( $post_id ) {
// check that we're intentionally saving / updating post
// get post content
// get transient
// calculate words and and it to transient count
// save transient
}
add_action( 'save_post', 'update_word_count_transient' );
This is just a concept and I haven't tested the code examples. Please add prefixes, validation, sanitizing, fix typos/bugs and fine tune functions as needed before using in production.
If you don't know php and are looking for a copy-and-paste solution, then please hire a professional to do the work for you
Tools to Manage Your WordPress Word Count
PublishPress is a great WordPress plugin for content, and it has a Content Checklist addon that allows you to choose a maximum and a minimum length of your WordPress content.
Here's how PublishPress and the Content Checklist works ...
More ideas at: https://www.ostraining.com/blog/wordpress/wordpress-word-count/
You can find the solutions for this here.
Or please use Google to find another way, the keyword can be "count word of the post wp" or same it.
Best regards,