Symptom. A minimum-word-count rule is wired into wp_insert_post_data. The callback counts words, finds the draft short, and does the only thing a filter can do: returns the array it was given. The post publishes anyway. The editor shows no error. The rule appears to exist and does nothing.
This is not a bug in the hook. It is a category error in the design. wp_insert_post_data filters the post array on its way to the database. It returns data. It cannot return a WP_Error, cannot abort the write, and cannot tell the block editor that a save was rejected. If your editorial policy lives there, it is advisory at best.
What follows traces the hook, shows what it can safely do, and moves enforcement to the place where a status change can actually be inspected and reverted.
Trace: what the hook actually receives
The developer reference documents the signature precisely:
apply_filters( 'wp_insert_post_data', array $data, array $postarr, array $unsanitized_postarr, bool $update )
Per the same page, $data is “slashed, sanitized, and processed post data,” $postarr is “sanitized (and slashed) but otherwise unmodified post data,” $unsanitized_postarr is the array as originally passed to wp_insert_post(), and $update is a boolean indicating whether an existing post is being updated. The filter is described as filtering “slashed post data just before it is inserted into the database.”
Two consequences follow directly from that description.
First, the hook fires on insert and update. The documentation’s own user-contributed note is blunt about it: “Contrary to the name, this hook also runs on post update and post delete.” That means every autosave, every revision write, every programmatic wp_update_post() call, and every REST save from the block editor passes through your callback. A word-count rule placed here is not a publish rule; it is a rule about every write to the posts table.
Second, the return value is the data array. There is no documented mechanism for returning an error, and the hook’s contract is a filter, not an action. If you return the array unchanged, the write proceeds. If you mutate post_status inside the filter, you are changing the data being written, not rejecting the operation — and you are doing so on every save, including saves the editor did not intend as a publish.
Trace: where the filter sits relative to the status transition
wp_insert_post() is the function that inserts or updates a post, and it updates when $postarr contains an ID. The developer reference for wp_update_post() shows that it ultimately calls wp_insert_post(), so both paths converge on the same filter.
The status transition is a separate mechanism. transition_post_status is documented as firing “when a post is transitioned from one status to another,” with three arguments: the new status, the old status, and the WP_Post object. The same page documents a status-specific variant, {$old_status}_to_{$new_status}, so draft_to_publish is a real hook you can attach to.
The retrieved source excerpts do not include the exact call site of wp_transition_post_status() relative to the wp_insert_post_data filter inside wp_insert_post(), so this article does not assert a precise ordering beyond what the documentation states: the filter runs inside wp_insert_post() before the database write, and the transition hook fires via wp_transition_post_status(). What matters for the design is not the microsecond ordering but the contract: one hook filters data, the other observes a status change on a WP_Post object you can inspect and act on.
Root cause: a filter cannot enforce
The failure is structural. Enforcement requires one of three things: refusing the write, returning an error the caller can surface, or reverting the state change after the fact. wp_insert_post_data offers none of these. It offers a data array and expects a data array back.
There is a second, subtler problem with counting words inside this filter. The $data['post_content'] you receive is the stored content, which for block-editor posts includes block comment delimiters and for classic posts may include shortcodes. A word count computed against that string is not the same as a word count computed against what the editor sees rendered. The documentation does not define a canonical word-count function, and this article does not invent one. The point is that the counting decision is separate from the hook decision: even if you count correctly, the filter still cannot reject the save.
What the filter is good for
Normalization. The developer reference includes a user-contributed example that decodes HTML entities from post_title inside the filter:
function filter_post_name( $data, $postarr, $unsanitized_postarr ) {
$data['post_title'] = html_entity_decode( $data['post_title'] );
return $data;
}
add_filter( 'wp_insert_post_data', 'filter_post_name', 10, 3 );
That is the correct shape of use: transform a field, return the array, scope it to a post type. The same page notes that you must pass 2 as the $accepted_args argument to reach $postarr, and that the post ID is available as $postarr['ID']. If you need $update, pass 4.
A scoped normalization callback looks like this:
add_filter( 'wp_insert_post_data', function ( $data, $postarr, $unsanitized_postarr, $update ) {
if ( 'post' !== $data['post_type'] ) {
return $data;
}
$data['post_title'] = trim( html_entity_decode( $data['post_title'] ) );
return $data;
}, 10, 4 );
Note what this does not do: it does not check word count, does not change status, and does not attempt to signal failure. It normalizes a field on every write, which is exactly what a data filter is for.
Fix: enforce at the status transition
Move the policy to transition_post_status or its status-specific variant. The documentation’s user-contributed notes are explicit that transition_post_status also fires on ordinary updates where the status does not change, so a bailout on $old_status === $new_status is required if you want to act only on real transitions.
The following is a labeled hypothetical, not a tested deployment. It illustrates the mechanism, not a recommended threshold:
add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) {
if ( 'publish' !== $new_status || 'publish' === $old_status ) {
return;
}
if ( 'post' !== $post->post_type ) {
return;
}
$word_count = str_word_count( wp_strip_all_tags( $post->post_content ) );
if ( $word_count < 300 ) {
// Revert to draft. This is a state change, not a filter return.
wp_update_post( array(
'ID' => $post->ID,
'post_status' => 'draft',
) );
}
}, 10, 3 );
Three things to notice. The callback bails when the status is not changing to publish, so it does not fire on every save. It bails on non-post types. And when it acts, it calls wp_update_post() to change status — a different mechanism than returning a filtered array. That call will itself pass through wp_insert_post_data and through transition_post_status, so the callback must be idempotent: the second transition is publish to draft, which the guard at the top already excludes.
This is not elegant. It is a revert, not a rejection, and the editor may see the post flip back to draft without a clear message. The documentation retrieved for this article does not describe how the block editor surfaces save failures, so this article does not claim a specific editor UX. What it does claim is that the revert is a real state change on a real WP_Post object, which is more than the filter can offer.
Verification
Confirm the hook is firing where you think it is. A minimal probe:
add_filter( 'wp_insert_post_data', function ( $data, $postarr, $unsanitized_postarr, $update ) {
error_log( sprintf(
'wp_insert_post_data: type=%s status=%s update=%s id=%s',
$data['post_type'],
$data['post_status'],
$update ? 'true' : 'false',
isset( $postarr['ID'] ) ? $postarr['ID'] : 'new'
) );
return $data;
}, 10, 4 );
Save a draft, publish it, and autosave it. The log should show multiple entries with different status and update values. If you expected one entry per publish, the log will correct that expectation.
Confirm the transition hook fires on the transition you care about:
add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) {
error_log( sprintf(
'transition: %s -> %s (id=%d, type=%s)',
$old_status, $new_status, $post->ID, $post->post_type
) );
}, 10, 3 );
Publish a draft and watch for a single draft -> publish line. Then update the published post without changing status and confirm you see a publish -> publish line — the documentation’s warning about the hook firing on non-transitions, demonstrated.
If you want to inspect the stored content your word-count logic will see, query it directly:
wp db query "SELECT ID, post_status, CHAR_LENGTH(post_content) AS len FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_modified DESC LIMIT 5;"
That gives you the raw length, not a word count, but it is enough to confirm that block markup is present in the stored string and that any count against it is a count against markup-inclusive content.
Debugging checklist
When a rule “does not work,” check these in order.
Hook arity. If your callback declares four parameters but add_filter() passes 10, 2, the third and fourth arguments are null. The documentation is explicit that you must pass 2 to reach $postarr; the same logic applies to 4 for $update.
Return value. A filter callback that returns nothing returns null, and the data array is lost. Every path through the callback must return $data.
Wrong hook for the job. If the requirement is “do not publish below N words,” the filter cannot satisfy it. Move the check to transition_post_status or draft_to_publish.
Missing bailout. A transition_post_status callback without an $old_status === $new_status guard fires on every update, including the revert you just performed, which is how you get a loop.
Counting the wrong string. $post->post_content contains block delimiters and shortcodes. If your threshold was chosen against rendered prose, the stored count will not match it. Decide which string the rule governs before you pick a number.
The general principle is not specific to word counts. Any editorial rule that must prevent a state change belongs at a hook that observes the state change. Any rule that must normalize a field belongs at a data filter. wp_insert_post_data is the second kind. Using it as the first kind produces a rule that logs, mutates, and returns — and publishes anyway.