#28135 closed defect (bug) (fixed)
wp_insert_post returns error for custom post type in 3.9.0
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 4.0 | Priority: | normal |
Severity: | major | Version: | 3.9 |
Component: | Posts, Post Types | Keywords: | good-first-bug has-patch |
Focuses: | Cc: |
Description
I'm trying to insert post for custom post type by function wp_insert_post it returns error. But functions still works, post is added to database.
This is my code:
$current_user = wp_get_current_user(); $name = $_REQUEST['paymetn_m_name'] . ' ' . $_REQUEST['paymetn_m_sname']; $post = array( 'post_title' => $name, 'post_type' => 'orders', 'post_status' => 'publish', 'post_author' => $current_user->ID ); wp_insert_post( $post );
This is error:
Notice: Trying to get property of non-object in /Users/capit0l/Sites/technostore/wp-includes/post.php on line 2187
Notice: Trying to get property of non-object in /Users/capit0l/Sites/technostore/wp-includes/post.php on line 2187
Problem is in this function:
function _count_posts_cache_key( $type = 'post', $perm = '' ) { $cache_key = 'posts-' . $type; if ( 'readable' == $perm && is_user_logged_in() ) { $post_type_object = get_post_type_object( $type ); if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) { $cache_key .= '_' . $perm . '_' . get_current_user_id(); } } return $cache_key; }
As a temporary solution I added @ in front of wp_insert_post function.
For extra information, i'm using OS X 10.9.2, PHP Version 5.4.24.
Attachments (4)
Change History (18)
#2
@
11 years ago
- Milestone changed from Awaiting Review to 3.9.1
#4
@
11 years ago
As SergeyBiryukov pointed out I was able to get this notice only if the post type is registered after wp_insert_post()
.
We need a post_type_exists()
check inside wp_insert_post()
. I find that that this function creates a post even if the $post_type
is registered nowhere. And this inserted post has the non-registered post type in the database.
I am interested to submit a patch for this but how is this error handled? Is a WP_Error
object returned, fallback to default post
type or _doing_it_wrong()
? I think WP_Error
is the right option.
#7
@
11 years ago
$post_type_object = get_post_type_object( $type );
is NULL
if post type doesn't exists.
@ew_holmes patch worked, just added post type slug into the message.
This ticket was mentioned in IRC in #wordpress-dev by tannermoushey. View the logs.
11 years ago
#9
@
11 years ago
Was going to commit 28135.2.diff and 28135_test.diff, but then realized we already have a ticket for that: #27335 (closed as maybelater).
Let's just avoid the notice introduced in [27081].
#11
@
11 years ago
- Keywords needs-patch good-first-bug added; has-patch needs-testing removed
We should just check if $post_type_object
is not empty in _count_posts_cache_key()
.
#12
@
11 years ago
- Keywords has-patch added; needs-patch removed
Another PHP notice is triggered on MS setups:
Notice: Trying to get property of non-object in /var/www/html/wp-includes/ms-blogs.php on line 877
The 28135.diff patch fixes both of them.
The function was added in [27081], moving for investigation.
Sounds like your code needs to wait until
register_post_type()
fires, but if this didn't cause a warning before, we should probably fix it.