$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>users - How to count total words for posts published by one author?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

users - How to count total words for posts published by one author?

matteradmin11PV0评论

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.

Share Improve this question edited Oct 25, 2018 at 9:07 p oneland asked Oct 25, 2018 at 8:07 p onelandp oneland 171 silver badge8 bronze badges 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
Add a comment  | 

3 Answers 3

Reset to default 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

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

Post a comment

comment list (0)

  1. No comments so far