• Hi!

    I’ve been looking for a plugin that analyzes the content of the posts, extracts the most used words, and exports them, but I couldn’t find anything. I saw some plugins but they are pretty old and don’t work with the current WordPress version.
    Is there any tool in the WordPress API that I could use to get this or do I have to build a complete development in PHP in order to get this information?

    Thanks, regards!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The below is not for production but just a quick and dirty example:

    <?php
    
    $results = $wpdb->get_results( "SELECT post_content FROM wp_posts WHERE post_type IN ('post') AND post_status IN ('publish') ");
    $res = implode(' ',array_column($results, 'post_content'));
    
    /* https://stackoverflow.com/a/46874560/2476069 */
    function most_frequent_words($string, $stop_words = [], $limit = 10) {
        $string = strtolower($string); // Make string lowercase
        $words = str_word_count($string, 1); // Returns an array containing all the words found inside the string
        $words = array_diff($words, $stop_words); // Remove black-list words from the array
        $words = array_count_values($words); // Count the number of occurrence
        arsort($words); // Sort based on count
        print_r( array_slice($words, 0, $limit)); // Limit the number of words and returns the word array
    	
    }
    most_frequent_words(strip_tags($res), array('the','a','to','and','of','for') );
    
    die();
    ?>

    Even filtering out a handful of common words, my result is a bunch of (to me) useless words counted from my test site.

    The above is more an example that in my opinion, you’ll need to filter out a lot more words, possibly limit results to larger words, although that could be an issue if you are limiting say word to 5+ letters but you talk about SPAM a lot, then it isn’t counted.

    I saw some plugins but they are pretty old and don’t work with the current WordPress version.

    Did you try them? Most likely they are just full of SQL queries so they should still work. Might be better not to reinvent the wheel here.

    Good luck!

    Hey, juliamb, besides tugbucket’s excellent advice, you could also examine and edit the code of the old word count plugins to fit your needs: https://plugins.trac.wordpress.org/browser/word-stats/

    Thread Starter juliamb

    (@juliamb)

    Thank you so much for your answers and the code example, @tugbucket!

    Yeah, I tried some of the old plugins I found but they weren’t working, so I discarded them. I didn’t think of edit their code, I was doing initial research to see if there is some direct solution, but I guess I could work around with them. Like you said @tugbucket, I don’t want to re-invent the wheel hahaha

    The Word Stats plugin is one of the plugins I tried, @plantprogrammer, I think I would start with it if I finally decided to edit the code of an existing plugin so it works on my site. This plugin looks really neat, it’s a pitty is no longer maintained.

    Thanks again!
    Regards

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘GGet the most used words in a WordPress blog’ is closed to new replies.