Adding word counts to the archive page Dark _ □ ✕
  • Posts
  • About
  • Debug
Post

Adding word counts to the archive page

Published
2026-09-01 13:30 UTC (precision: second)
Author
Kelly Adams
Source feed
Ubergeek Kelly's World [exclude this blog]
Link
https://www.kgadams.net/sitenews/adding-word-counts-to-the-archive-page
Length
4442 runes
Extracted text
I spent a bit of time on Monday adding a PHP code snippet to my WordPress site that outputs a total monthly word count and average word count per post to the archives page. This is far from a complex bit of code, and I used AI to get the basics, but here it is. PHP snippet to add word counts A few notes: This code only works on WordPress it is best used with a plugin like WPCode or Code Snippets to allow such small blocks of code to be added without interfering with WordPress’ built in code functionality or requiring editing of any core theme files A transient cache is in place for the two database results used to improve performance and reduce database impact; adjust the set_transient timeout values to suit your needs (currently set to 12 hours) the average is generated inefficiently: the monthly post count is already generated as part of the page logic. But I’m not sure where that value is or how to access it, so I re-generated it with another SQL query there is a potential logic flaw in the way the ‘word_count’ and ‘count’ values are retrieved. They can be updated separately, potentially having an out of date post count when the word_count changes. I will likely look at forcing them both to be updated if either of them changes in a future code update <?php add_filter('get_archives_link', 'wp_add_word_count_to_monthly_archives', 10, 6); /** * Appends total word count to monthly archive links. * * @param string $link_html The archive HTML link content. * @param string $url URL to archive. * @param string $text Archive text description (e.g., "August 2026"). * @param string $format Link format ('html', 'option', etc.). * @param string $before Content to prepend. * @param string $after Content to append. * @return string Modified HTML link content. */ function wp_add_word_count_to_monthly_archives($link_html, $url, $text, $format, $before, $after) { // Only apply this to HTML formatted links (like <li>) to prevent breaking dropdown options if ('html' !== $format) { return $link_html; } // Attempt to parse the month and year from the archive text link $timestamp = strtotime($text); if (!$timestamp) { return $link_html; } $year = date('Y', $timestamp); $month = date('m', $timestamp); // Use a transient cache to prevent heavy database queries on every page load $transient_key = 'wp_archive_word_count_' . $year . '_' . $month; $transient_key_count = 'wp_archive_count_' . $year . '_' . $month; $word_count = get_transient($transient_key); $count = get_transient($transient_key_count); if (false === $word_count) { global $wpdb; // SQL query to calculate approximate word count by counting spaces in post_content $word_count = $wpdb->get_var($wpdb->prepare( "SELECT SUM( LENGTH(post_content) - LENGTH(REPLACE(post_content, ' ', '')) + 1 ) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND YEAR(post_date) = %d AND MONTH(post_date) = %d", $year, $month )); // Default to 0 if no words found $word_count = $word_count ? (int) $word_count : 0; // Cache the result for 12 hours set_transient($transient_key, $word_count, 12 * HOUR_IN_SECONDS); } if (false === $count) { global $wpdb; // get the count of posts for the month $count = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND YEAR(post_date) = %d AND MONTH(post_date) = %d", $year, $month )); // Default to 0 if no posts found $count = $count ? (int) $count : 0; // Cache the result for 12 hours set_transient($transient_key_count, $count, 12 * HOUR_IN_SECONDS); } // Format the number for cleaner display (e.g., 12,500 words) $formatted_word_count = number_format($word_count); if ($count > 0) { $formatted_average = number_format($word_count / $count); } else { $formatted_average = 0; } // Append the word count right before the closing </a> tag $link_html = str_replace('</a>', "</a> ($formatted_word_count words; $formatted_average avg. words per post)", $link_html); return $link_html; } The output on an archive page after adding this snippet to WordPress looks like this: The good news is that I can see my average post length now is under 1,000 words. The bad news is that I can see my average post length sometimes dips down below 500 words per post, which is a bit sparse. Regardless, it makes me happy to have this information, and now you have it too! AI was used to generate the header image (ChatGPT) and most of the code (Google search AI)
Stored topics

Every stored assignment is listed, including rows below the current cutoff (0.75), so a missing tag can be explained.

Concept Code Score Meter Margin Origin Path
economy, business and finance economy-business-and-finance 0.97 3.51 ↑derived from 20000225 economy, business and finance
products and services products-and-services 0.97 3.51 ↑derived from 20000225 economy, business and finance > products and services
computing and information technology computing-and-information-technology 0.97 3.51 direct economy, business and finance > products and services > computing and information technology
science and technology science-and-technology 0.97 3.41 ↑derived from 20000763 science and technology
technology and engineering technology-and-engineering 0.97 3.41 ↑derived from 20000763 science and technology > technology and engineering
information technology and computer science information-technology-and-computer 0.97 3.41 direct science and technology > technology and engineering > information technology and computer science
software and applications software-and-applications 0.97 3.36 ↑derived from local:software-development economy, business and finance > products and services > computing and information technology > software and applications
Software development software-development 0.97 3.36 direct economy, business and finance > products and services > computing and information technology > software and applications > Software development
Information security information-security 0.88 1.96 direct science and technology > technology and engineering > information technology and computer science > Information security
Open source open-source 0.77 1.22 direct economy, business and finance > products and services > computing and information technology > software and applications > Open source
arts, culture, entertainment and media arts-culture-entertainment-and-media 0.74 1.04 ↑derived from local:blogging arts, culture, entertainment and media
mass media mass-media 0.74 1.04 ↑derived from local:blogging arts, culture, entertainment and media > mass media
social media social-media 0.74 1.04 ↑derived from local:blogging arts, culture, entertainment and media > mass media > social media
Blogging blogging 0.74 1.04 direct arts, culture, entertainment and media > mass media > social media > Blogging
artificial intelligence artificial-intelligence 0.69 0.78 direct science and technology > technology and engineering > information technology and computer science > artificial intelligence
arts and entertainment arts-and-entertainment 0.47 -0.11 ↑derived from 20000013 arts, culture, entertainment and media > arts and entertainment
literature literature 0.47 -0.11 direct arts, culture, entertainment and media > arts and entertainment > literature
crime, law and justice crime-law-and-justice 0.39 -0.45 ↑derived from 20000086 crime, law and justice
crime crime 0.39 -0.45 ↑derived from 20000086 crime, law and justice > crime
cyber crime cyber-crime 0.39 -0.45 direct crime, law and justice > crime > cyber crime
Cryptography cryptography 0.36 -0.58 direct science and technology > technology and engineering > information technology and computer science > Cryptography
society society 0.34 -0.68 ↑derived from 20001300 society
fundamental rights fundamental-rights 0.34 -0.68 ↑derived from 20001300 society > fundamental rights
privacy privacy 0.34 -0.68 direct society > fundamental rights > privacy
media and entertainment industry media-and-entertainment-industry 0.30 -0.85 ↑derived from 20000306 economy, business and finance > products and services > media and entertainment industry
books and publishing books-and-publishing 0.30 -0.85 direct economy, business and finance > products and services > media and entertainment industry > books and publishing
lifestyle and leisure lifestyle-and-leisure 0.26 -1.07 ↑derived from local:mmorpg lifestyle and leisure
leisure leisure 0.26 -1.07 ↑derived from local:mmorpg lifestyle and leisure > leisure
game game 0.26 -1.07 ↑derived from local:mmorpg lifestyle and leisure > leisure > game
video game video-game 0.26 -1.07 ↑derived from local:mmorpg lifestyle and leisure > leisure > game > video game
MMORPG mmorpg 0.26 -1.07 direct lifestyle and leisure > leisure > game > video game > MMORPG
Self-hosting self-hosting 0.25 -1.09 direct economy, business and finance > products and services > computing and information technology > Self-hosting
social sciences social-sciences 0.21 -1.35 ↑derived from 20000747 science and technology > social sciences
history history 0.21 -1.35 direct science and technology > social sciences > history
scientific research scientific-research 0.15 -1.73 ↑derived from 20000739 science and technology > scientific research
scientific exploration scientific-exploration 0.15 -1.73 ↑derived from 20000739 science and technology > scientific research > scientific exploration
space exploration space-exploration 0.15 -1.73 direct science and technology > scientific research > scientific exploration > space exploration
environment environment 0.14 -1.82 ↑derived from 20000418 environment
climate change climate-change 0.14 -1.82 direct environment > climate change
politics and government politics-and-government 0.11 -2.14 direct politics and government
sport sport 0.09 -2.27 ↑derived from 20001183 sport
competition discipline competition-discipline 0.09 -2.27 ↑derived from 20001183 sport > competition discipline
eSports esports 0.09 -2.27 direct sport > competition discipline > eSports
natural science natural-science 0.08 -2.43 ↑derived from 20000719 science and technology > natural science
biology biology 0.08 -2.43 direct science and technology > natural science > biology
labour labour 0.08 -2.46 direct labour
education education 0.06 -2.72 direct education
film industry film-industry 0.06 -2.73 direct economy, business and finance > products and services > media and entertainment industry > film industry
Tabletop gaming tabletop-gaming 0.05 -2.97 direct lifestyle and leisure > leisure > game > Tabletop gaming
health health 0.04 -3.15 ↑derived from 20000458 health
disease and condition disease-and-condition 0.04 -3.15 ↑derived from 20000458 health > disease and condition
mental health and disorder mental-health-and-disorder 0.04 -3.15 direct health > disease and condition > mental health and disorder
health treatment and procedure health-treatment-and-procedure 0.04 -3.26 ↑derived from 20000465 health > health treatment and procedure
diet diet 0.04 -3.26 direct health > health treatment and procedure > diet
animation animation 0.03 -3.35 direct arts, culture, entertainment and media > arts and entertainment > animation
religion religion 0.03 -3.40 direct religion
culture culture 0.03 -3.53 direct arts, culture, entertainment and media > culture
consumer goods consumer-goods 0.03 -3.53 ↑derived from 20001160 economy, business and finance > products and services > consumer goods
handicrafts handicrafts 0.03 -3.53 direct economy, business and finance > products and services > consumer goods > handicrafts
streaming service streaming-service 0.03 -3.54 direct economy, business and finance > products and services > media and entertainment industry > streaming service
conflict, war and peace conflict-war-and-peace 0.02 -3.71 direct conflict, war and peace
family family 0.02 -3.85 direct society > family
visual arts visual-arts 0.02 -3.87 ↑derived from 20000036 arts, culture, entertainment and media > arts and entertainment > visual arts
photography photography 0.02 -3.87 direct arts, culture, entertainment and media > arts and entertainment > visual arts > photography
television television 0.02 -3.92 direct arts, culture, entertainment and media > mass media > television
human interest human-interest 0.02 -3.96 ↑derived from 20000498 human interest
award and prize award-and-prize 0.02 -3.96 direct human interest > award and prize
hobby hobby 0.02 -3.96 direct lifestyle and leisure > leisure > hobby
exercise and fitness exercise-and-fitness 0.01 -4.25 direct lifestyle and leisure > wellness > exercise and fitness
wellness wellness 0.01 -4.25 ↑derived from 20001239 lifestyle and leisure > wellness
weather weather 0.01 -4.30 ↑derived from 20001128 weather
weather forecast weather-forecast 0.01 -4.30 direct weather > weather forecast
disaster, accident and emergency incident disaster-accident-and-emergency-incident 0.01 -4.45 direct disaster, accident and emergency incident
music music 0.01 -4.50 direct arts, culture, entertainment and media > arts and entertainment > music
lifestyle lifestyle 0.01 -4.53 ↑derived from 20001257 lifestyle and leisure > lifestyle
house and home house-and-home 0.01 -4.53 ↑derived from 20001257 lifestyle and leisure > lifestyle > house and home
gardening gardening 0.01 -4.53 direct lifestyle and leisure > lifestyle > house and home > gardening
travel and tourism travel-and-tourism 0.01 -4.67 direct lifestyle and leisure > leisure > travel and tourism
podcast podcast 0.01 -4.76 direct economy, business and finance > products and services > media and entertainment industry > podcast
card game card-game 0.01 -4.80 direct lifestyle and leisure > leisure > game > card game
board game board-game 0.01 -4.99 direct lifestyle and leisure > leisure > game > board game
public health public-health 0.01 -5.28 direct health > public health
food and drink enthusiasm food-and-drink-enthusiasm 0.00 -5.60 direct lifestyle and leisure > leisure > hobby > food and drink enthusiasm

← Back to posts

feedtagger 0.1.0-dev model: modernbert-base-zeroshot-v2.0/int8@all-s256 2115 posts 0 unclassified min score 0.75