WordPress 3.1 upgrade “invalid post type” error

wtf

Custom post type breaks after upgrade to 3.1

I routinely upgraded a client website to WordPress 3.1 today and upon clicking the menu title of my custom post type in the back end, I was met with the error "Invalid Post Type". On the front end side the permalinks were not working either; it was a real mess. OK, stay calm, I backed up the database, so it will be fixable.

3.1 upgrade also causes broken permalinks

The code snippet below also includes the flush_rewrite_rules function which I had recently added to my custom post types article as a result of the permalink structure not working normally. This client's website had been working OK without this function, so I suspect one of the minor 3.0.x releases introduced a code change which makes this function necessary in the event of broken custom post type permalinks.

Change uppercase post type to lowercase

After some searching I came across a post about uppercase custom post type problems with the release candidate of WordPress 3.1. In the case of my client's site, the post type was "Products", so I changed it to "product" in the following line of code

register_post_type( 'product', $args);
// add the following function if you are getting 
// a 'Page not found' error from your permalink
flush_rewrite_rules( false );

Update database with new post_type name

None of this will work if your custom posts are sitting in the database with the old post_type value. The following MySQL command can be used to run and 'find and replace' to update all your table rows. I did this via phpMyAdmin by entering the command below into the "Run SQL query/queries on database" field under the view for the wp_posts table.

UPDATE wp_posts
SET post_type = REPLACE(post_type, 'Products', 'product')

Update your template files

Lastly, you'll need to update any template files that are calling on the post_type "Products" to now call for "products" in your wp_query functions or wherever else is necessary.

Let me know if you have had a similar experience and if these steps fixed your problems

3 Responses to “WordPress 3.1 upgrade “invalid post type” error”

  1. Luke America says:

    If this doesn’t solve your category/tag link list 404’s (pulldown and top level menus) when you have a custom permalink for Blog Posts, try this “action” function. It’s worked on every site I’ve tried (including multi-sites) WITHOUT having to tinker with .htaccess, change the existing permalink structure, or disable any installed plugins.

    The problem is with 3.1’s canonical redirect. Sometimes, just disabling the redirect fixes the pblm. But, if this doesn’t solve it for you, here’s a more extensive alternative hotfix link. Just put the code in your theme’s functions.php file.

    http://wpcodesnippets.info/blog/how-to-fix-the-wp-3-1-custom-permalinks-bug.html

  2. Steve says:

    Hey man, I tried the SQL query solution and at first nothing happened until I realized that I had a second database so I ran it on the second one and that got it! Success!

  3. Thank you so much for this, I have been avoiding updating because the last time I tried it ruin all my custom post types. Now it got everything running smooth on the latest version.