Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#27335 closed defect (bug) (maybelater)

wp_insert_post() inserts posts of unregistered post types

Reported by: brocheafoin's profile brocheafoin Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.8.1
Component: Posts, Post Types Keywords:
Focuses: Cc:


if you run something like this:

    'post_title' => 'My Example',
    'post_type' => 'bogus_post_type_that_is_not_registered'

Your post is inserted into the database and wp_insert_post() returns a post ID. I would have expected this to be an error.

Change History (5)

#1 @danielbachhuber
10 years ago

Thanks for the bug report, brocheafoin. Out of curiosity, how'd you end up with a bogus custom post type, and what sort of problems did this behavior cause for you?

#2 @nacin
10 years ago

I can't find my previous answer on this subject (may have been a ticket, but can't find it) — but basically, we deliberately don't restrict post types for compatibility reasons. Some were using the post_type field well before register_post_type() existed, and even then, it doesn't need to be an error condition. Most of WordPress won't recognize the post type, but wp_insert_post() is a low-level API (it's also a high-level API; yes, it's a problem) and doesn't care.

Adding an error now will likely cause other problems. I could picture a situation where someone is registering a post type, but somehow does so (at least in some situations) after a wp_insert_post() call.

#3 @brocheafoin
10 years ago

  • Resolution set to maybelater
  • Status changed from new to closed

@danielbachhuber: I'm writing a plugin that inserts a bunch of posts and I made a typo in my wp_insert_post() call. No posts were showing up, yet no errors either. Found the posts in the DB with my typo'ed post types. It's really a developer-only bug.

@nacin: I figured this had to have been discussed already, but I couldn't find it either.

wp_insert_post() is a low-level API (it's also a high-level API; yes, it's a problem)

I guess that's the main issue. And it's a can of worms probably best left unopened. I'm closing.

#4 @SergeyBiryukov
10 years ago

  • Milestone Awaiting Review deleted

#5 @knutsp
10 years ago

Some sort of warning or notice could be given.

Note: See TracTickets for help on using tickets.