- Joined
- Sep 3, 2014
- Messages
- 6,244
- Likes
- 13,131
- Degree
- 9
You may recall me mentioning that I wrote a piece of code to count up all of the words of every post and page and specific custom fields so I could track some data about content length. I never shared it because it ended up too specific for my site with a lot of custom fields and other things going on. I'll try to generalize that and share it soon.
Like that chunk of code, you don't want to leave this one uncommented and running every page load because it's pretty intensive on the database. But once in a blue moon you may want to uncomment it and take a glance at how your content is doing.
I keep returning to my oldest content from when I first put my main site together. My philosophy has changed quite a bit since the early days. I want to return to those posts and make sure each is reaching a certain word count threshold.
For instance, I may have some posts that are image and video heavy but only have 100 words of content. While that may be fine with Google now, I want to pre-defend from any future Panda issues, for example. I want each of my posts delivering value and not be hit-and-runs. I know there's probably 20 posts I could stand to improve.
But finding them was the issue. So here's something I just put together, largely based on work someone else did on Stack Overflow and character counting, that I had to adjust to strip out HTML tags and then count only words.
The end result is you end up with a new column in your Posts section of your dashboard in Wordpress that shows you the word count of the post, and you can order by it too, which allowed me to see my shortest posts in a heart beat.
I used the developer tools of my browser to change the title, author, and categories text for the screen shot, but that's the actual new Length column and two of my longer posts.
Here's the code you can safely copy and paste into your functions.php file to enable this on your site:
Again, I recommend deleting or commenting out this entire set of filters and functions when you're not actively using them. On big sites it could slow down the front-end for your users significantly each time you go into the Posts section of the dashboard.
Happy content optimizing!
EDIT: I'm noticing it's ordering strangely. It might be with the way I stripped the tags. I'll have instances where the ordering will be slightly out of order, where the word counts might be out of order like:
Like that chunk of code, you don't want to leave this one uncommented and running every page load because it's pretty intensive on the database. But once in a blue moon you may want to uncomment it and take a glance at how your content is doing.
I keep returning to my oldest content from when I first put my main site together. My philosophy has changed quite a bit since the early days. I want to return to those posts and make sure each is reaching a certain word count threshold.
For instance, I may have some posts that are image and video heavy but only have 100 words of content. While that may be fine with Google now, I want to pre-defend from any future Panda issues, for example. I want each of my posts delivering value and not be hit-and-runs. I know there's probably 20 posts I could stand to improve.
But finding them was the issue. So here's something I just put together, largely based on work someone else did on Stack Overflow and character counting, that I had to adjust to strip out HTML tags and then count only words.
The end result is you end up with a new column in your Posts section of your dashboard in Wordpress that shows you the word count of the post, and you can order by it too, which allowed me to see my shortest posts in a heart beat.
I used the developer tools of my browser to change the title, author, and categories text for the screen shot, but that's the actual new Length column and two of my longer posts.
Here's the code you can safely copy and paste into your functions.php file to enable this on your site:
PHP:
//-----
// Post Word Count Checker - Keep Commented Unless Using
//-----
// Add Length Column
add_filter('manage_post_posts_columns', function ( $columns )
{
$_columns = [];
foreach( (array) $columns as $key => $label )
{
$_columns[$key] = $label;
if( 'title' === $key )
$_columns['ryu_post_content_length'] = __( 'Length' );
}
return $_columns;
} );
// Fill Column With Word Counts
add_action( 'manage_post_posts_custom_column', function ( $column_name, $post_id )
{
if ( $column_name == 'ryu_post_content_length')
echo str_word_count( strip_tags( get_post( $post_id )->post_content ) );
}, 10, 2 );
// Make Column Orderable
add_filter( 'manage_edit-post_sortable_columns', function ( $columns )
{
$columns['ryu_post_content_length'] = 'ryu_post_content_length';
return $columns;
} );
// Order Through Proper Filter
add_filter( 'posts_orderby', function( $orderby, \WP_Query $q )
{
$_orderby = $q->get( 'orderby' );
$_order = $q->get( 'order' );
if(
is_admin()
&& $q->is_main_query()
&& 'ryu_post_content_length' === $_orderby
&& in_array( strtolower( $_order ), [ 'asc', 'desc' ] )
) {
global $wpdb;
$orderby = " LENGTH( {$wpdb->posts}.post_content ) " . $_order . " ";
}
return $orderby;
}, 10, 2 );
Again, I recommend deleting or commenting out this entire set of filters and functions when you're not actively using them. On big sites it could slow down the front-end for your users significantly each time you go into the Posts section of the dashboard.
Happy content optimizing!
EDIT: I'm noticing it's ordering strangely. It might be with the way I stripped the tags. I'll have instances where the ordering will be slightly out of order, where the word counts might be out of order like:
- 925
- 1050
- 1000
- 1100
- 1125