I have been using the following code on my other wordpress sites to show a list of all posts in the website oredered by word count. However, when I tried to use the same code in my new website wp 5.9 (I believe due to gutenberg) it shows the list and a number after the title, but the numbers are really small like 1 2 3, nowhere near the actual word count.
require_once('wp-blog-header.php');
$args = array('post_type' => 'post', 'posts_per_page' => 500, 'post_status' => 'publish');
$results = get_posts($args);
$nb_words = array();
$title = array();
foreach($results as $r){
$nb_words[] = str_word_count( strip_tags( $r->post_content ));
$title[] = $r->post_title;
}
array_multisort($nb_words, SORT_ASC, SORT_NUMERIC, $title);
echo "<ol>";
for ($i = 0; $i < sizeof($title); $i++) {
echo '<li>'.$title[$i] . ' => ' . $nb_words[$i] . '<br></li>';
}
echo '</ol>';
Could you suggest why the str_word_count doesn't work here?