🧼 Deleting all Orphaned posts of certain Post Types in WordPress

If you’ve ever uninstalled a plugin or removed a custom post type in WordPress and started seeing odd PHP notices, you’re not alone. Recently, I ran into a warning that popped up on every page load in the admin dashboard:

PHP Notice: map_meta_cap was called incorrectly. The post type eventhub is not registered…

Sound familiar?

This happens when WordPress tries to check capabilities (like edit_post) for a post type that no longer exists. The problem: orphaned entries in your database. If a plugin registered a post type like eventhub, and you removed that plugin without deleting its content, those posts remain — and WordPress doesn’t like that.

🧠 Why This Matters

Even though these leftover posts aren’t visible in the UI, they still exist in the wp_posts table. When WordPress sees them, it tries to load the post type logic — and fails, because that post type is no longer registered. This can clutter your debug log, slow things down, or even break some admin functionality.

🚀 The Solution: WP-CLI to the Rescue

Here’s a simple, modern WP-CLI command that safely removes all posts from a custom post type — even if that post type no longer exists in your codebase:

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

🔍 What This Command Does

  • wp post list --post_type='eventhub' --format=ids: This lists all post IDs of the now-orphaned eventhub type.
  • The $() syntax feeds those IDs into the next command.
  • wp post delete ... --force: Deletes all the listed posts permanently, skipping the trash bin.

Replace eventhub with whatever your orphaned post type is (like portfolio, project, customform, etc.).

✅ Cleanup Checklist

Here’s a quick checklist to avoid this kind of issue in the future:

  • Before removing a plugin or custom post type, check if it added a post type.
  • Use wp post list --post_type='your_post_type' to confirm if any data remains.
  • Back up your database before running destructive WP-CLI commands.
  • Consider writing a small migration script if you need to preserve the data elsewhere.

🛠 Bonus Tip

If you’re not sure what custom post types exist in your database, this command is a handy starting point:

wp db query "SELECT DISTINCT post_type FROM wp_posts;"

It will return all post types currently present in the database — even if they’re no longer registered in your theme or plugins.

📚 Learn More

💬 Final Thoughts

It’s 2025, and managing a modern WordPress site is easier than ever — if you keep your data clean. WP-CLI continues to be an essential tool in every developer’s toolbox. When in doubt, script it out.

Happy coding — and may your debug log always be silent. 🧘‍♂️


Posted

in

by

Tags: