I’m currently updating posts for one of my sites and I wanted to easily see the word count for each of my posts on the All Posts page in the WordPress dashboard (the /wp-admin/edit.php page).

WordPress doesn’t seem to provide an out of the box solution so with ChatGPT’s help, I created the necessary code.

Updated to work on posts and pages.

Here’s the end result:

Add a "Word Count" Column To The WP Backend (Works for Posts & Pages) 1
The Word Count Column should be standard, but as of January 2023, it’s not.

PHP Code To Show The Word Count Column in WordPress All Posts and All Pages

It’s reasonably straight-forward to achieve.

Simply add some code to your functions.php file OR, use the FREE Code Snippets plugin from WPCode.

I opted for the plugin route because I have various code snippets and this amazing little plugin keeps everything organized. It offers granular control over exactly where the code can execute and it allows me to switch on or off various code snippets as desired.

All for free! There is a pro version too. I might check that out at a later date.

By the way, the plugin used to be called Insert Headers and Footers. At time of writing, it has 1+ million installs, so it’s very popular.

Anyway, here’s the code snippet:

// Uses WordPress functionality to show a word count column for posts and pages in the backend

function add_word_count_column($columns) {
    $columns['word_count'] = 'Word Count';
    return $columns;
}
add_filter('manage_posts_columns', 'add_word_count_column');
add_filter('manage_pages_columns', 'add_word_count_column');

function show_word_count_column($name, $post_id) {
    if ($name === 'word_count') {
        $word_count = str_word_count(strip_tags(get_post_field('post_content', $post_id)));
        update_post_meta($post_id, 'word_count', $word_count);
        echo $word_count;
    }
}
add_action('manage_posts_custom_column', 'show_word_count_column', 10, 2);
add_action('manage_pages_custom_column', 'show_word_count_column', 10, 2);

function make_word_count_column_sortable($columns) {
    $columns['word_count'] = 'word_count';
    return $columns;
}
add_filter('manage_edit-post_sortable_columns', 'make_word_count_column_sortable');
add_filter('manage_edit-page_sortable_columns', 'make_word_count_column_sortable');

function word_count_column_orderby($query) {
    if (!is_admin() || !$query->is_main_query()) {
        return;
    }

    $orderby = $query->get('orderby');

    if ('word_count' === $orderby) {
        $query->set('meta_key', 'word_count');
        $query->set('orderby', 'meta_value_num');
        $query->set('word_count', true);
    }
}
add_action('pre_get_posts', 'word_count_column_orderby');

function word_count_column_request($vars) {
    if (isset($vars['word_count']) && $vars['word_count']) {
        $vars = array_merge($vars, array(
            'meta_key' => 'word_count',
            'orderby' => 'meta_value_num',
        ));
    }

    return $vars;
}
add_filter('request', 'word_count_column_request');

Here’s how to get the Word Count column working for the backend your WordPress site:

  1. Install and activate the free Code Snippets plugin
  2. On the side menu, click on Add Snippet
  3. Select Add Your Custom Code (New Snippet)
  4. Paste the code above into the Code Preview box
  5. Select PHP Snippet as the Code Type from the dropdown box
  6. Insure the Auto Insert option is selected from Insert Method
  7. Select Admin Only as the location
  8. Go back to the top, toggle the Inactive button to show Active
  9. Click Save Snippet
  10. Visit your Posts (all posts page) and you should see the Word Count column

If this doesn’t work, sorry, I cannot offer support. It works perfectly for me and I have a minimal setup.

If it doesn’t work for you, chances are that another plugin might be causing a conflict.

Enjoy!

Similar Posts