Counting WordPress Site Words
Counting all the words published on a WordPress site by directly accessing the database is a bit technical, but it can be done using SQL queries. You'll need access to your WordPress site's database (typically using phpMyAdmin or a similar tool). Here are the steps:
Important Note: Before making any changes to your database, it's strongly recommended to create a backup.
Access the Database: Log in to phpMyAdmin or your preferred database management tool.
Locate the WordPress Database: In most cases, your WordPress database is named something like
wp_database
, where "wp" is a prefix you chose during installation. Click on this database to select it.Find the Posts Table: WordPress stores post content, including the text of articles, in a table typically named
wp_posts
. Click on this table to select it.Write a SQL Query: You can use SQL to count the words in each post and then sum those counts to get the total word count. Here's a SQL query to do this:
sqlSELECT SUM(LENGTH(`post_content`) - LENGTH(REPLACE(`post_content`, ' ', '')) + 1) AS word_count FROM `wp_posts` WHERE `post_type` = 'post' AND `post_status` = 'publish';
This query calculates the word count for all published posts in your WordPress site.
Run the Query: Click the "SQL" tab in phpMyAdmin, paste the SQL query into the text box, and click "Go" to execute the query.
View the Word Count: The query result will display the total word count of all published posts on your WordPress site.
Keep in mind that this query will only count the words in the main content of your posts and not in other areas like post titles, excerpts, or metadata. If you want a more detailed word count or want to include other areas, you may need to modify the query accordingly.
Please exercise caution when accessing your database and ensure that you have proper backups before making any changes.
Certainly! Let's break down the SQL query step by step:
sqlSELECT SUM(LENGTH(`post_content`) - LENGTH(REPLACE(`post_content`, ' ', '')) + 1) AS word_count
FROM `wp_posts`
WHERE `post_type` = 'post'
AND `post_status` = 'publish';
SELECT SUM(...): This part of the query is responsible for selecting and summing up the word counts. It calculates the word count for each post and then adds them together to get the total word count. The result of this summation is given an alias name 'word_count' for clarity.
LENGTH(
post_content
): This part calculates the length (number of characters) of thepost_content
field in each post.post_content
is where the main text content of your WordPress posts is stored.LENGTH(REPLACE(
post_content
, ' ', '')): This part calculates the length of thepost_content
field after removing all spaces. It does this by using theREPLACE
function to replace all spaces (' ') in thepost_content
with an empty string (''). This effectively removes all spaces, leaving only the characters.+ 1: After calculating the length of
post_content
with spaces and without spaces, we add 1 to account for the last word in the post because there won't be a space after the last word.FROM
wp_posts
: This specifies the table from which we are selecting data, in this case, thewp_posts
table. This table stores information about your WordPress posts, including their content.WHERE
post_type
= 'post': This is a filter that ensures we only consider posts with apost_type
of 'post'. In WordPress, different types of content (like pages, attachments, etc.) have differentpost_type
values, and we want to focus on regular blog posts.AND
post_status
= 'publish': Another filter that restricts the query to only consider posts with apost_status
of 'publish'. This ensures we're counting words only in published posts, not drafts or other statuses.
In summary, this SQL query calculates the total word count of all published blog posts on your WordPress site. It does this by finding the length of the content of each post (including spaces), subtracting the length of the content with spaces removed, and adding 1 to account for the last word. Then, it sums up these word counts to give you the total word count for all published posts.
You're correct that the post_content
field in WordPress includes not only the actual text content but also various HTML and WordPress-specific tags, including blocks introduced by the WordPress block editor like <!-- wp:paragraph -->
. If you want to count only the words within the actual text content and exclude any HTML or WordPress-specific tags, you can modify the SQL query.
Here's an updated SQL query that attempts to count words within the actual text content while ignoring tags:
sqlSELECT SUM(LENGTH(
REPLACE(
REPLACE(
REPLACE(`post_content`, '<!--', ''),
'-->', ''),
'</p>', ' ')
) - LENGTH(REPLACE(REPLACE(REPLACE(REPLACE(`post_content`, '<!--', ''), '-->', ''), '</p>', ' '), ' ', '')) + 1) AS word_count
FROM `wp_posts`
WHERE `post_type` = 'post'
AND `post_status` = 'publish';
This updated query uses additional REPLACE
functions to remove HTML comments (<!-- ... -->
) and the </p>
tag (assuming it's used to close paragraphs). Then, it proceeds to count the words in the cleaned content.
Please note that this query still has limitations and may not handle all possible cases perfectly, especially if your content contains complex HTML structures or other special formatting. However, it should give you a closer approximation of the word count within the actual text content while disregarding HTML and WordPress-specific tags.
To display the word count dynamically in a WordPress post that updates whenever you create a new post, you can use a combination of custom PHP code and a WordPress plugin. Here's a high-level overview of the steps:
Create a Custom WordPress Plugin:
First, you'll need to create a custom WordPress plugin. This plugin will contain the PHP code to calculate and display the word count. You can create a new folder in your WordPress
plugins
directory and create a PHP file within it, or you can create a separate plugin using a code editor.Write the PHP Code:
In your custom plugin file, write the PHP code that calculates the word count of all published posts. You can use the SQL query we discussed earlier. Here's an example of what your PHP code might look like:
php<?php /* Plugin Name: Word Count Plugin Description: Display the total word count of all published posts. */ function display_word_count() { global $wpdb; $query = "SELECT SUM(LENGTH(REPLACE(REPLACE(REPLACE(post_content, '<!--', ''), '-->', ''), '</p>', ' ')) - LENGTH(REPLACE(REPLACE(REPLACE(REPLACE(post_content, '<!--', ''), '-->', ''), '</p>', ' '), ' ', '')) + 1) AS word_count FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish'"; $result = $wpdb->get_var($query); echo 'Total Word Count: ' . $result; } add_shortcode('word_count', 'display_word_count');
This code defines a WordPress shortcode
[word_count]
that will display the total word count of all published posts.Activate the Plugin:
Upload your custom plugin to the WordPress
plugins
directory or use the WordPress plugin editor. Activate the plugin through the WordPress admin interface.Insert the Shortcode into Posts:
In your WordPress posts, where you want to display the word count, simply insert the shortcode
[word_count]
. When you publish a new post, this shortcode will dynamically calculate and display the total word count of all published posts.
Now, whenever you create a new post or update an existing one, the word count displayed in your post will automatically update to reflect the current total word count of all published posts.
Remember to customize the plugin code and shortcode output to match your site's styling and formatting requirements.
Expanding the word count display to include the number of published posts and the average words per post is a great idea. You can modify the custom WordPress plugin we created earlier to include these additional statistics. Here's the modified plugin code:
php<?php
/*
Plugin Name: Word Count Plugin
Description: Display word count, number of published posts, and average words per post.
*/
function display_word_count() {
global $wpdb;
// Calculate the total word count
$total_word_count_query = "SELECT SUM(LENGTH(REPLACE(REPLACE(REPLACE(post_content, '<!--', ''), '-->', ''), '</p>', ' ')) - LENGTH(REPLACE(REPLACE(REPLACE(REPLACE(post_content, '<!--', ''), '-->', ''), '</p>', ' '), ' ', '')) + 1) AS word_count FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish'";
$total_word_count = $wpdb->get_var($total_word_count_query);
// Calculate the number of published posts
$published_posts_query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish'";
$published_posts = $wpdb->get_var($published_posts_query);
// Calculate the average words per post
$average_words_per_post = ($published_posts > 0) ? round($total_word_count / $published_posts) : 0;
echo '<p>Total Word Count: ' . $total_word_count . '</p>';
echo '<p>Number of Published Posts: ' . $published_posts . '</p>';
echo '<p>Average Words Per Post: ' . $average_words_per_post . '</p>';
}
add_shortcode('word_count_stats', 'display_word_count');
This code has been modified to calculate and display the following statistics:
Total Word Count: This is the same as before, displaying the total word count of all published posts.
Number of Published Posts: It counts the total number of published posts.
Average Words Per Post: It calculates the average number of words per published post.
The plugin now uses a [word_count_stats]
shortcode to display these statistics in your posts. To use it, simply insert [word_count_stats]
where you want these statistics to appear in your WordPress posts.
After updating the plugin with this code, activate it, and you'll have a shortcode that dynamically displays the total word count, the number of published posts, and the average words per post in your WordPress posts.
When creating a custom WordPress plugin, the name you give to the plugin file is not critical; however, it's a good practice to make it descriptive and related to the functionality of your plugin. A common convention is to use lowercase letters and underscores for the file name.
For the plugin code we've discussed earlier, you can name the plugin file something like word_count_stats_plugin.php
. Here's how the file name relates to the code:
word_count_stats_plugin.php
is the file containing the PHP code for your plugin.Inside the plugin file, you define the plugin name and description using comments like this:
php/* Plugin Name: Word Count Plugin Description: Display word count, number of published posts, and average words per post. */
The key part is the "Plugin Name" specified in the comments, which is what will be displayed in the WordPress admin interface when you activate the plugin.
So, in summary, you can name your plugin file something like word_count_stats_plugin.php
, but you can choose a different name if it better reflects the functionality of your plugin. Just make sure the PHP file extension .php
is included, and it should be placed in the wp-content/plugins/
directory of your WordPress installation.
The positioning of the [word_count_stats]
shortcode within your WordPress post is determined by where you insert it within the post's content editor. If it's appearing at the top of your post when you've added it at the bottom, it's possible that your WordPress theme or another plugin is affecting the way shortcodes are processed and displayed.
To ensure that the [word_count_stats]
shortcode appears at the bottom of your post, you can follow these steps:
Edit the Post: Open the post where you want to display the word count statistics in the WordPress admin.
Switch to Text (HTML) Mode: In the post editor, make sure you're working in the Text or HTML mode, not the Visual mode. You can typically switch between these modes using tabs or buttons at the top of the editor.
Move the Shortcode: Locate the
[word_count_stats]
shortcode in the editor's text. Cut it (Ctrl + X on Windows or Command + X on Mac).Position It at the Bottom: Scroll down to the very end of the post's text in the Text/HTML editor and paste (Ctrl + V on Windows or Command + V on Mac) the shortcode there. Make sure there's no other content after the shortcode.
Update the Post: Click the "Update" button to save your changes.
By moving the [word_count_stats]
shortcode to the end of the post content in the Text/HTML editor and ensuring there's no other content after it, you should have it displayed at the bottom of your post.
If it still doesn't appear at the bottom of your post as expected, there might be specific theme or plugin-related considerations that need to be addressed. In such cases, you might need to consult your theme's documentation or consider seeking support from the theme/plugin developer.
Certainly, you can format the numbers in a nice table with dots for readability. To achieve this, you can update the plugin code to use the number_format
PHP function and HTML for table formatting. Here's the modified plugin code:
php<?php
/*
Plugin Name: Word Count Plugin
Description: Display word count, number of published posts, and average words per post in a table.
*/
function display_word_count() {
global $wpdb;
// Calculate the total word count
$total_word_count_query = "SELECT SUM(LENGTH(REPLACE(REPLACE(REPLACE(post_content, '<!--', ''), '-->', ''), '</p>', ' ')) - LENGTH(REPLACE(REPLACE(REPLACE(REPLACE(post_content, '<!--', ''), '-->', ''), '</p>', ' '), ' ', '')) + 1) AS word_count FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish'";
$total_word_count = $wpdb->get_var($total_word_count_query);
// Calculate the number of published posts
$published_posts_query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish'";
$published_posts = $wpdb->get_var($published_posts_query);
// Calculate the average words per post
$average_words_per_post = ($published_posts > 0) ? round($total_word_count / $published_posts) : 0;
// Format the numbers with dots for readability
$total_word_count_formatted = number_format($total_word_count, 0, '.', '.');
$average_words_per_post_formatted = number_format($average_words_per_post, 0, '.', '.');
// Create an HTML table to display the statistics
$output = '<table>';
$output .= '<tr><th>Statistic</th><th>Value</th></tr>';
$output .= '<tr><td>Total Word Count</td><td>' . $total_word_count_formatted . '</td></tr>';
$output .= '<tr><td>Number of Published Posts</td><td>' . $published_posts . '</td></tr>';
$output .= '<tr><td>Average Words Per Post</td><td>' . $average_words_per_post_formatted . '</td></tr>';
$output .= '</table>';
echo $output;
}
add_shortcode('word_count_stats', 'display_word_count');
In this updated code:
- The
number_format
function is used to format the numbers with dots for readability. - An HTML table is created to display the statistics in a structured and visually pleasing way.
Now, when you use the [word_count_stats]
shortcode in your posts, it will display the statistics in a table format with numbers formatted using dots for readability.
To display the output in Dutch, you can update the text within the HTML table to be in Dutch. Here's the modified plugin code with the table headers and labels in Dutch:
php<?php
/*
Plugin Name: Word Count Plugin
Description: Toon het woordaantal, aantal gepubliceerde berichten en gemiddeld aantal woorden per bericht in een tabel.
*/
function display_word_count() {
global $wpdb;
// Calculate the total word count
$total_word_count_query = "SELECT SUM(LENGTH(REPLACE(REPLACE(REPLACE(post_content, '<!--', ''), '-->', ''), '</p>', ' ')) - LENGTH(REPLACE(REPLACE(REPLACE(REPLACE(post_content, '<!--', ''), '-->', ''), '</p>', ' '), ' ', '')) + 1) AS word_count FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish'";
$total_word_count = $wpdb->get_var($total_word_count_query);
// Calculate the number of published posts
$published_posts_query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish'";
$published_posts = $wpdb->get_var($published_posts_query);
// Calculate the average words per post
$average_words_per_post = ($published_posts > 0) ? round($total_word_count / $published_posts) : 0;
// Format the numbers with dots for readability
$total_word_count_formatted = number_format($total_word_count, 0, '.', '.');
$average_words_per_post_formatted = number_format($average_words_per_post, 0, '.', '.');
// Create an HTML table to display the statistics in Dutch
$output = '<table>';
$output .= '<tr><th>Statistiek</th><th>Waarde</th></tr>';
$output .= '<tr><td>Totaal Woordaantal</td><td>' . $total_word_count_formatted . '</td></tr>';
$output .= '<tr><td>Aantal Gepubliceerde Berichten</td><td>' . $published_posts . '</td></tr>';
$output .= '<tr><td>Gemiddeld Aantal Woorden Per Bericht</td><td>' . $average_words_per_post_formatted . '</td></tr>';
$output .= '</table>';
echo $output;
}
add_shortcode('word_count_stats', 'display_word_count');
In this updated code, I've translated the table headers and labels into Dutch, so when you use the [word_count_stats]
shortcode in your posts, the output will be in Dutch.