0

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.

2
  • it's the task of custom coding. whoever might have a time, might post it. otherwise, you can always look for plugins if there exists any.
    – T.Todua
    Commented Oct 25, 2018 at 8:51
  • Can you help with the code to achieve this?
    – p oneland
    Commented Oct 25, 2018 at 10:07

3 Answers 3

1

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

0

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 ...

  • Install the PublishPress plugin.
  • Get the Content Checklist addon from PublishPress.com. Download the Content Checklist files.
  • In your WordPress site, go to "Plugins", then "Add New" and install Content Checklist.
  • Go to "PublishPress", then "Checklist" in your WordPress admin area.
  • Look for the “Number of words” field. Here you can choose a minimum and maximum word count:

More ideas at: https://www.ostraining.com/blog/wordpress/wordpress-word-count/

2
  • Seems you didn't get my point...
    – p oneland
    Commented Oct 25, 2018 at 9:08
  • I don't need the count for a single post, neither to limit the words for a post. Just wanna show the total word count of one author's posts in the author archive page.
    – p oneland
    Commented Oct 25, 2018 at 9:17
0

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,

4
  • I really did that. "count word of the post wp" will be easy. But to get " total word count of one author's all posts" might be not from Google.
    – p oneland
    Commented Oct 25, 2018 at 9:12
  • If so, you must get all post of that author after count word for each the post of the author and sum all results. Commented Oct 25, 2018 at 9:27
  • Can you help me with the code to achieve this?
    – p oneland
    Commented Oct 25, 2018 at 10:08
  • Sorry, I don't have free time, please research to do it. Thank you very much ^^ Commented Oct 25, 2018 at 10:30

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.