How To Add Word Count Column with Sorting to WordPress Admin

⏲️ Estimated reading time: 4 min

WordPress HelpZone give you all about the code we created for showing and sorting Word Count in the admin dashboard.


Add Column with Sorting to WordPress Admin. Learn how to display the words counting for each post in your WordPress admin dashboard, along with the ability to sort posts by words counting in ascending or descending order.


Display and Sort Word Count in WordPress Admin Dashboard

WordPress by default doesn’t show words counting in the admin panel which can be frustrating for bloggers, content managers, or SEO experts who care about content length. In this guide, we’ll show you how to add a custom words counting column to the Posts list in the dashboard, complete with sorting functionality.

You’ll also learn how to store the words counting as custom meta data, and bulk-update existing posts to populate the words counting without needing to manually resave them.


Add Word Count Column

Why Add Word Count to the Admin Dashboard?

While the WordPress editor shows the words counting when editing a post, it’s not visible at a glance when managing multiple posts. With a custom column in the post list, you can:

  • Monitor post length quickly.
  • Sort long-form and short-form posts.
  • Improve SEO and content planning.
  • Identify thin content or overly long articles.

Full Code to Add Word Count Column and Enable Sorting

Below is a complete snippet you can place in your functions.php file or in a custom plugin:

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

// Display Word Count
add_action('manage_posts_custom_column', 'display_word_count_column', 10, 2);
function display_word_count_column($column_name, $post_id) {
    if ($column_name === 'word_count') {
        $word_count = get_post_meta($post_id, '_custom_word_count', true);
        if (!$word_count) {
            $content = get_post_field('post_content', $post_id);
            $word_count = str_word_count(strip_tags($content));
            update_post_meta($post_id, '_custom_word_count', $word_count);
        }
        echo $word_count;
    }
}

// Make the column sortable
add_filter('manage_edit-post_sortable_columns', 'make_word_count_sortable');
function make_word_count_sortable($columns) {
    $columns['word_count'] = 'word_count';
    return $columns;
}

// Sorting logic
add_action('pre_get_posts', 'sort_posts_by_word_count');
function sort_posts_by_word_count($query) {
    if (is_admin() && $query->is_main_query() && $query->get('orderby') === 'word_count') {
        $query->set('meta_key', '_custom_word_count');
        $query->set('orderby', 'meta_value_num');
    }
}

// Save word count on post save
add_action('save_post', 'save_word_count_meta');
function save_word_count_meta($post_id) {
    if (wp_is_post_revision($post_id) || get_post_type($post_id) !== 'post') return;
    $content = get_post_field('post_content', $post_id);
    $word_count = str_word_count(strip_tags($content));
    update_post_meta($post_id, '_custom_word_count', $word_count);
}

// Admin notice for bulk update
add_action('admin_notices', 'word_count_bulk_update_notice');
function word_count_bulk_update_notice() {
    if (isset($_GET['word_count_update']) && current_user_can('manage_options')) {
        word_count_bulk_update();
        echo '<div class="notice notice-success is-dismissible"><p>Word counts updated for all published posts.</p></div>';
    } else {
        echo '<div class="notice notice-info is-dismissible"><p><a href="' . admin_url('edit.php?word_count_update=1') . '" class="button">Update Word Count for All Posts</a></p></div>';
    }
}

// Bulk update word count for existing posts
function word_count_bulk_update() {
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'fields' => 'ids'
    );
    $posts = get_posts($args);
    foreach ($posts as $post_id) {
        $content = get_post_field('post_content', $post_id);
        $word_count = str_word_count(strip_tags($content));
        update_post_meta($post_id, '_custom_word_count', $word_count);
    }
}

How to Use It

  1. Paste the code into your functions.php file.
  2. Go to Posts > All Posts in your admin dashboard.
  3. Click the blue button labeled “Update Word Count for All Posts.”
  4. You’ll now see a sortable “Word Count” column.

That’s it! You’ve just extended the WordPress dashboard with a powerful new tool.


🏷️ Tags: wordpress tutorial, wordpress admin, custom column, words counting, sortable posts, functions.php, wordpress developer, seo tools, wp backend, wordpress tips

Report an issue (max 5 words):

Only logged-in users can submit reports.


Discover more from HelpZone

Subscribe to get the latest posts sent to your email.

Want to support us? Let friends in on the secret and share your favorite post!

Photo of author

Flo

How To Add Word Count Column with Sorting to WordPress Admin

Published

Update

Welcome to HelpZone.blog, your go-to hub for expert insights, practical tips, and in-depth guides across technology, lifestyle, business, entertainment, and more! Our team of passionate writers and industry experts is dedicated to bringing you the latest trends, how-to tutorials, and valuable advice to enhance your daily life. Whether you're exploring WordPress tricks, gaming insights, travel hacks, or investment strategies, HelpZone is here to empower you with knowledge. Stay informed, stay inspired because learning never stops! 🚀

👍 Like us on Facebook!

Closing in 10 seconds

Leave a Reply