Delete all posts of a post type from the WordPress database

I found myself having a really odd problem on one of my development sites. At some point, I must have installed a plugin that creates a post type called wpcode but then I got rid of it. Since then, on every dashboard load, I would get this error:

PHP Notice:  Function map_meta_cap was called <strong>incorrectly</strong>. 
The post type <code>wpcode</code> is not registered, so it may not be reliable to check the capability <code>edit_post</code> against a post of that type. 
Please see <a href="https://wordpress.org/documentation/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information.
(This message was added in version 4.4.0.) in .../app/public/wp-includes/functions.php on line 5865

I tried to ignore but there was no ignoring it. So here’s a quick WP-CLI command that will force delete any posts from a custom post type if needed. Just replace POST_TYPE_HERE with your chosen post type, in my case, wpcode.

wp post delete $(wp post list --post_type='POST_TYPE_HERE' --format=ids) --force

It’ll let you know what posts it deleted (by way of post ID), which may or may not be helpful. I just copied this verbatim from the WP-CLI docs example and it worked like a charm.

The relief I felt at no longer seeing this error in my DEBUG LOG was palpable. WP-CLI for the win, y’all.

Did you like this article? Get articles like this straight in your inbox for easy reading. 🙂