I can no longer post or even save a draft, getting the error “A post type mismatch has been detected.”

In my version of WordPress this error will come from line 46 of wp-admin/post.php, which has:

if ( isset( $_POST['post_type'] ) && $post && $post_type !== $_POST['post_type'] ) {
    wp_die( __( 'A post type mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 );
}

The code before that looks pretty straight forward. It just checks the database for the post object based on the post ID that is submitted with the form.

You might gain more insight by trying the following. Edit the post in question. Right click the browser and click inspect element (in Chrome). Click the “network” tab, then find the “all” sub tab. Now, leave the dev tools window open, and click Publish. An item should show up in the list called post.php. Ignore if it says post.php?action=edit etc.

Click on post.php and on the right click the Headers tab. Ensure that under “General” it says Request Method: POST. Then under Form Data, if its hard to read, click view parsed. Otherwise, it should say post_type: post. In addition you should see post_ID: {some integer}.

if post_ID or post_type is incorrect then this will cause your issue. Another thing to look out for is if there are 2 items with the same name. Check the end of the list to see if either of them is submitted again. Submitted the same index twice is fine in general but probably the second one will take precedence and it could be incorrect.

I would guess that this probably won’t reveal anything. But it can show that the form data is likely being rendered properly and sent to the server properly by your web browser. Assuming nothing looks incorrect here, it’s probably something server side that happens after the data is correctly submitted.

The next thing to check would be to add two lines of code above line 46. However, you have to add the code after you load the page and before you hit submit. And you’ll have to remove the code afterwards otherwise you won’t be able to view or save any posts.

echo '<pre>' . htmlspecialchars( print_r($_POST, true) ) . '</pre>';
exit;

if ( isset( $_POST['post_type'] ) && $post && $post_type !== $_POST['post_type'] ) {
    wp_die( __( 'A post type mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 );
}

When I hit publish, I get the following…

Array
(
    [_wpnonce] => f5557dcf89
    [_wp_http_referer] => /wp-starter/wp-admin/post.php?post=1&action=edit
    [user_ID] => 1
    [action] => editpost
    [originalaction] => editpost
    [post_author] => 1
    [post_type] => post
    [original_post_status] => publish
    [referredby] => http://localhost:8080/wp-starter/wp-admin/post.php?post=1&action=edit
    [_wp_original_http_referer] => http://localhost:8080/wp-starter/wp-admin/post.php?post=1&action=edit
    [post_ID] => 1
    [meta-box-order-nonce] => a98dcd6a27
    [closedpostboxesnonce] => 1033a61a51
    [post_title] => Hello world!

    .... (plus a bunch more)
)

This array should more or less contain the same data that was in the network tab under Form Data.

The important part is that the server is recognizing the post_ID and the post_type, and they are both valid.

It’s also possible that your server is truncating the input variables to a maximum number. By default this number is quite high but if a form sends say over 1000 points of data to the server, it might be ignoring them, which if the post_type was ignored, then this will cause the error you are seeing.