# Minimum Word Count to Publish Posts

> Stops posts shorter than a minimum number of words from being published, in both the block editor and the classic editor, to keep thin content off the site.

- Type: PHP
- Runs: Run everywhere
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: editorial, word-count, publishing
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/minimum-word-count-to-publish/

## Code

```php
// Minimum number of words a post needs before it can be published.
$wpide_min_words = 300;

$wpide_count_words = function ( $content ) {
    return count( preg_split( '/\s+/u', trim( wp_strip_all_tags( strip_shortcodes( (string) $content ) ) ), -1, PREG_SPLIT_NO_EMPTY ) );
};

add_filter( 'rest_pre_insert_post', function ( $prepared, $request ) use ( $wpide_min_words, $wpide_count_words ) {
    $post_id = isset( $prepared->ID ) ? (int) $prepared->ID : 0;
    $status  = isset( $prepared->post_status ) ? $prepared->post_status : ( $post_id ? get_post_status( $post_id ) : 'draft' );

    if ( ! in_array( $status, array( 'publish', 'future' ), true ) ) {
        return $prepared;
    }

    $content = isset( $prepared->post_content ) ? $prepared->post_content : ( $post_id ? get_post_field( 'post_content', $post_id ) : '' );

    if ( $wpide_count_words( $content ) < $wpide_min_words ) {
        return new WP_Error( 'wpide_too_short', sprintf( 'This post needs at least %d words before it can be published.', $wpide_min_words ), array( 'status' => 400 ) );
    }

    return $prepared;
}, 10, 2 );

add_filter( 'wp_insert_post_data', function ( $data ) use ( $wpide_min_words, $wpide_count_words ) {
    if ( ! is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || 'post' !== $data['post_type'] || 'publish' !== $data['post_status'] ) {
        return $data;
    }

    if ( $wpide_count_words( wp_unslash( $data['post_content'] ) ) < $wpide_min_words ) {
        $data['post_status'] = 'draft';

        add_filter( 'redirect_post_location', function ( $location ) {
            return add_query_arg( 'wpide_too_short', 1, remove_query_arg( 'message', $location ) );
        } );
    }

    return $data;
} );

add_action( 'admin_notices', function () use ( $wpide_min_words ) {
    if ( isset( $_GET['wpide_too_short'] ) ) {
        echo '<div class="notice notice-error"><p>' . esc_html( sprintf( 'The post was saved as a draft because it has fewer than %d words.', $wpide_min_words ) ) . '</p></div>';
    }
} );
```

## Notes

Drafts can still be saved at any length. Change the minimum at the top of the snippet.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Minimum Word Count to Publish Posts" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
